0
我想为我的用户控件ToggleImageButton创建一个名为IsChecked的依赖项属性。我只是想能够设置并获得ToggleButton中的IsChecked属性。依赖项属性似乎没有反映ToggleButton的IsChecked属性的实际值。请有人帮忙。在用户控件中创建依赖关系的问题
这里是隐藏文件我的代码:
public partial class ToggleImageButton : UserControl
{
public ToggleImageButton()
{
InitializeComponent();
}
public ImageSource Image
{
get { return (ImageSource)GetValue(ImageProperty); }
set { SetValue(ImageProperty, value); }
}
public static readonly DependencyProperty ImageProperty =
DependencyProperty.Register("Image", typeof(ImageSource), typeof(ToggleImageButton), new UIPropertyMetadata(null));
public Boolean IsChecked
{
get { return (Boolean)GetValue(IsCheckedProperty); }
set { SetValue(IsCheckedProperty, value); }
}
}
这里是XAML文件:
<UserControl x:Class="MyControls.ToggleImageButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Name="UC">
<Grid Margin="0,0,0,0">
<ToggleButton Margin="0,0,0,0" IsChecked="{Binding RelativeSource={RelativeSource Self}, Path=Source.IsChecked, Mode=TwoWay}">
<StackPanel Orientation="Horizontal" Margin="0,0,0,0">
<Image Source="{Binding ElementName=UC, Path=Image}"
Stretch="Fill"
Width="{Binding RelativeSource={RelativeSource Self}, Path=Source.PixelWidth}"
Height="{Binding RelativeSource={RelativeSource Self}, Path=Source.PixelHeight}"
Margin="0,0,0,0"/>
</StackPanel>
</ToggleButton>
</Grid>
你的UC没有IsChecked属性 – 2011-12-12 16:52:34
好吧,我刚找到解决方案。 IsChecked绑定在xaml文件中,我错误地使用了RelativeSource,我已经从高度和宽度属性中复制并粘贴了,我只是修正了isChecked绑定,因此它是IsChecked =“{Binding ElementName = UC,Path = IsChecked} – Alex
对不起,UC确实有一个IsChecked属性,我在粘贴代码时一定会忽略它。谢谢 – Alex