새로 만든 usercontrol에 대한 사용자 지정 스타일을 설정하려고하는데 오류가 발생합니다. "속성 '속성의 값을 형식의 개체로 변환 할 수 없습니다. '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);
}
}
그러나 그것은 작동하지 않았다 이미지가 표시 어쨌든 앞서 언급 한 바와 같이 같은 오류가 발생하지 않은 : 나는 또한이 시도.
아이디어가 있으십니까? 감사합니다.
- 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>
고마워, 나는 거의 다 왔다고 생각해. ImageSource_PropertyChanged를 다시 작성하여 빌드하고 어쩌면 이것이 잘못 될 수 있을까요? 하지만 내 스타일 (내가보기 위해 위에 추가 한 기능)이 비활성화되었을 때 내 버튼에 적용되지 않습니다. 어떤 아이디어? private static void ImageSource_PropertyChanged (DependencyObject source, DependencyPropertyChangedEventArgs e) { ((MyButton) 소스) .tehImage.Source = (ImageSource) e.NewValue; } – 4imble