2013-06-18 3 views
0

Я пытаюсь создать «DropDownButton» с иконкой в ​​нем, и я хочу, чтобы иметь возможность установить источник значка через прикрепленное свойство (я обнаружил, что это (только ?) способ сделать это). Но по какой-то причине все, что я пробовал, терпит неудачу, лучшее, что я мог получить, это пустой контейнер Image.Невозможно получить/установить пользовательское прикрепленное свойство

Я думал, что это выглядело довольно хорошо, но теперь я получаю эти ошибки:

The local property "Image" can only be applied to types that are derived from "IconButton". 
The attachable property 'Image' was not found in type 'IconButton'. 
The attached property 'IconButton.Image' is not defined on 'Button' or one of its base classes. 

Я, вероятно, делать это совершенно неправильно (я пытался и редактирования в течение 2 часов в настоящее время), но я просто знаю, что должен быть способ сделать это.

Соответствующий код приведен ниже, если кто-нибудь может даже указать мне в правильном направлении, что было бы замечательно!

EDIT: Обновленный код, все еще не проблема

Теперь я получаю эту ошибку в журнале отладки:

System.Windows.Data Error: 40 : BindingExpression path error: 'Image' property not found on 'object' ''ContentPresenter' (Name='')'. BindingExpression:Path=Image; DataItem='ContentPresenter' (Name=''); target element is 'Image' (Name=''); target property is 'Source' (type 'ImageSource') 

ImageButton.cs (спасибо VIV):

class ImageButton : Button 
{ 
    public static readonly DependencyProperty ImageProperty = 
     DependencyProperty.Register("Image", typeof(ImageSource), typeof(ImageButton), 
     new FrameworkPropertyMetadata(null, 
      FrameworkPropertyMetadataOptions.Inherits | FrameworkPropertyMetadataOptions.AffectsRender)); 

    public ImageSource Image 
    { 
     get { return (ImageSource)GetValue(ImageProperty); } 
     set { SetValue(ImageProperty, value); } 
    } 
} 

ImageButton Стиль:

<Style TargetType="{x:Type Controls:ImageButton}" x:Key="FormIconDropDownButton"> 
    <Setter Property="Margin" Value="5" /> 
    <Setter Property="Padding" Value="10,5,4,5" /> 
    <Setter Property="ContentTemplate"> 
     <Setter.Value> 
      <DataTemplate DataType="{x:Type Controls:ImageButton}"> 
       <Grid> 
        <Grid.ColumnDefinitions> 
         <ColumnDefinition Width="Auto" /> 
         <ColumnDefinition /> 
         <ColumnDefinition Width="Auto" /> 
        </Grid.ColumnDefinitions> 

        <Image Style="{StaticResource FormButtonIcon-Small}" 
          Source="{Binding Image, RelativeSource={RelativeSource TemplatedParent}}"/> 

        <TextBlock Grid.Column="1" 
           Text="{Binding Content, RelativeSource={RelativeSource TemplatedParent}}" 
           VerticalAlignment="Center" 
           Margin="0,0,9,0"/> 

        <Path Grid.Column="2" 
          Fill="Black" 
          Data="M 0 0 L 3.5 4 L 7 0 Z" 
          VerticalAlignment="Center"/> 
       </Grid> 
      </DataTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

В окне XAML:

<Controls:ImageButton Content="Hello" 
           Style="{StaticResource FormIconDropDownButton}" 
           Image="{StaticResource Icon-Small-Locations}" /> 
+0

** IconButton ** должен ** EXTEND ** ** **, где вы объявите DependencyProperty. В Style Write Target = "IconButton". Например, в вашем случае: '