在用户控件中创建依赖项时遇到问题

发布于 2024-12-20 23:05:06 字数 1722 浏览 5 评论 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>

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

懵少女 2024-12-27 23:05:06

好的,我刚刚找到了解决方案。对于 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 –

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文