2009-10-20 121 views
3

我想爲我新建的用戶控件設置一個自定義樣式,但是我收到錯誤:「無法將屬性'屬性'中的值轉換爲類型的對象'System.Windows.DependencyProperty'。」依賴屬性不工作,試圖通過樣式設置器

我想我已經建立了依賴屬性,但它似乎不是這種情況,所以我做了一些研究,並補充說:

public static readonly DependencyProperty ImageSourceProperty = 
    DependencyProperty.Register("ImageSource", typeof(BitmapSource), typeof(Image)); 

,使這個: - MyButton.Xaml.Cs -

namespace Client.Usercontrols 
{ 
    public partial class MyButton : UserControl 
    { 
     public static readonly DependencyProperty ImageSourceProperty = 
      DependencyProperty.Register("ImageSource", typeof(BitmapSource), typeof(Image)); 

     public MyButton() 
     { 
      InitializeComponent();    
     } 


     public event RoutedEventHandler Click; 

     void onButtonClick(object sender, RoutedEventArgs e) 
     { 
      if (this.Click != null) 
       this.Click(this, e); 
     } 

     BitmapSource _imageSource; 
     public BitmapSource ImageSource 
     { 
      get { return _imageSource; } 
      set 
      { 
       _imageSource = value; 
       tehImage.Source = _imageSource; 
      } 
     } 
    } 
} 

這不幸的是不起作用。我也試過這個:

public BitmapSource ImageSource 
{ 
    get { return (BitmapSource)GetValue(MyButton.ImageSourceProperty); } 
    set 
    { 
     SetValue(ImageSourceProperty, value); 
    } 
} 

但是,這並沒有工作,圖像沒有顯示,併產生與前面提到的相同的錯誤。

任何想法? 尊敬Kohan。

- MyButton.Xaml -

<UserControl x:Class="Client.Usercontrols.MyButton" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" MinHeight="30" MinWidth="40" 
    DataContext="{Binding RelativeSource={RelativeSource Self}}"> 


    <Button Width="Auto" HorizontalAlignment="Center" Click="onButtonClick"> 

     <Border CornerRadius="5" BorderThickness="1" BorderBrush="Transparent" > 
      <Grid> 
       <Image Name="tehImage" Source="{Binding ImageSource}" /> 
       <TextBlock Name="tehText" Text="{Binding Text}" Style="{DynamicResource ButtonText}" /> 
      </Grid> 
     </Border> 

    </Button> 
</UserControl> 

- myButton的風格 -

<Style TargetType="{x:Type my:MyButton}" > 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type my:MyButton}"> 
       <ContentPresenter /> 
       <ControlTemplate.Triggers> 
        <Trigger Property="IsEnabled" Value="false"> 
         <Setter Property="ImageSource" Value="../Images/Disabled.png" />       
        </Trigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

回答

3

的標準格式依賴屬性是(我已經在你的信息添加):

public BitmapSource ImageSource 
{ 
    get { return (BitmapSource)GetValue(ImageSourceProperty); } 
    set { SetValue(ImageSourceProperty, value); } 
} 

/* Using a DependencyProperty as the backing store for ImageSource. 
    This enables animation, styling, binding, etc... */ 

public static readonly DependencyProperty ImageSourceProperty = 
     DependencyProperty.Register("ImageSource", 
      typeof(BitmapSource), 
      typeof(MyButton), 
      new UIPropertyMetadata(null) 
     ); 

它看起來像你也試圖通過依賴項屬性傳遞給名爲「tehImage」的對象的ImageSource。您可以將其設置爲使用PropertyChangedCallback自動更新...這意味着只要屬性更新,就會自動調用更新。

因此屬性代碼變爲:

public BitmapSource ImageSource 
{ 
    get { return (BitmapSource)GetValue(ImageSourceProperty); } 
    set { SetValue(ImageSourceProperty, value); } 
} 

/* Using a DependencyProperty as the backing store for ImageSource. 
    This enables animation, styling, binding, etc... */ 

public static readonly DependencyProperty ImageSourceProperty = 
     DependencyProperty.Register("ImageSource", 
      typeof(BitmapSource), typeof(MyButton), 
      new UIPropertyMetadata(null, 
       ImageSource_PropertyChanged 
      ) 
     ); 

private static void ImageSource_PropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e) 
{ 
    ((MyButton)source).tehImage.ImageSource = (ImageSource)e.NewValue 
} 
與正確註冊依賴屬性

希望這將有助於你縮小問題(甚至修復它)

+0

謝謝,我想我快到了。我不得不重新修改ImageSource_PropertyChanged來構建它,也許這是它出錯的地方?但我的樣式(我現在已添加上面看到)不適用於我的按鈕,當它被設置爲禁用。有任何想法嗎? private static void ImageSource_PropertyChanged(DependencyObject source,DependencyPropertyChangedEventArgs e) {((MyButton)source).tehImage.Source =(ImageSource)e.NewValue; } – 4imble 2009-10-21 11:57:54

4
我看到

最大的問題是,你註冊財產Image而不是所擁有您的UserControl。更改爲:

public static readonly DependencyProperty ImageSourceProperty = 
      DependencyProperty.Register("ImageSource", typeof(BitmapSource), typeof(MyButton)); 

如果這樣不起作用,則需要查看您的XAML。

+0

這似乎已經停止了我的風格現在發生的錯誤,但它現在停止我的usercontrol從工作,它已經從它使用的所有地方消失。我已經添加了usercontrol的Xaml。 – 4imble 2009-10-20 10:18:33

1

設置的DataContext你的用戶控件:

public MyButton() 
{ 
    InitializeComponent(); 
    DataContext = this; 
} 

另外,如果你不能做到這一點(因爲DataContext設置到另一個對象,例如),你可以在你的XAML做到這一點:

<UserControl x:Class="Client.Usercontrols.MyButton" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" MinHeight="30" MinWidth="40" 
    DataContext="{Binding RelativeSource={RelativeSource Self}}" 
    x:Name="MyControl"> 


    <Button Width="Auto" HorizontalAlignment="Center" Click="onButtonClick"> 

     <Border CornerRadius="5" BorderThickness="1" BorderBrush="Transparent" > 
      <Grid> 
       <Image Name="tehImage" Source="{Binding ElementName=MyControl, Path=ImageSource}" /> 
       <TextBlock Name="tehText" Text="{Binding ElementName=MyControl, Path=Text}" Style="{DynamicResource ButtonText}" /> 
      </Grid> 
     </Border> 

    </Button> 
</UserControl> 
0

實現源的正確方法對於我認爲用戶控件中的圖像不是BitmapSouce。最簡單也是最好的方法(根據我的說法)是使用Uri。

你所依賴的屬性更改爲這個(同時也定義更改回調事件):

ImageSourceProperty = DependencyProperty.Register(
    "ImageSource", typeof (Uri), typeof (MyButton), 
    new FrameworkPropertyMetadata(new PropertyChangedCallback(OnImageSourceChanged))); 

和屬性,以這樣的:

public Uri ImageSource 
{ 
    get 
    { 
      return (Uri)GetValue(ImageSourceProperty); 
    } 
    set 
    { 
      SetValue(ImageSourceProperty, value); 
    } 
} 

如果您回電是這樣的:

private static void OnImageSourceChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) 
{ 
    MyButton hsb = (MyButton)sender; 

    Image image = hsb.tehImage; 
    image.Source = new BitmapImage((Uri) e.NewValue); 
}