在用户控件中创建依赖项时遇到问题
我正在尝试为我的用户控件 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>
I'm trying to create a dependency property called IsChecked for my user control ToggleImageButton. I just want to be able to set and get the IsChecked Property in the ToggleButton. The dependency property doesn't seem to reflect the actual value of the ToggleButton's IsChecked property. Please somebody help.
Here is my code behind file:
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); }
}
}
And here is the xaml file:
<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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,我刚刚找到了解决方案。对于 xaml 文件中的 IsChecked 绑定,我错误地使用了从 height 和 width 属性复制并粘贴的relativesource。我刚刚修复了 isChecked 绑定,因此它是 IsChecked="{Binding ElementName=UC, Path=IsChecked} 并且它有效 -
Ok, I just found the solution. For the IsChecked binding in the xaml file, I was mistakenly using RelativeSource which I had copied and pasted from the height and width properties. I just fixed the isChecked binding so it is IsChecked="{Binding ElementName=UC, Path=IsChecked} and it worked –