如何使用另一个依赖项属性作为回退值



我正在创建一个用户控件,它是一个带有图像的切换按钮。当选中切换按钮和未选中切换按钮时,我对图像有单独的依赖属性。

Xaml:

   <ToggleButton IsChecked="{Binding Checked, Mode=TwoWay}">
        <Image>
            <Image.Style>
                <Style TargetType="Image">
                    <Setter Property="Source" Value="{Binding CheckedImage}"></Setter>
                        <DataTrigger Binding="{Binding IsChecked}" Value="False">
                            <Setter Property="Source" Value="{Binding UncheckedImage}"></Setter>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Image.Style>
        </Image>
    </ToggleButton>

代码隐藏:

public partial class ImageToggleButton : UserControl
{
    public ImageToggleButton()
    {
        InitializeComponent();
        this.DataContext = this;
    }
    public bool Checked
    {
        get { return (bool)GetValue(IsCheckedProperty); }
        set { SetValue(IsCheckedProperty, value); }
    }
    // Using a DependencyProperty as the backing store for IsChecked.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty IsCheckedProperty =
        DependencyProperty.Register("Checked", typeof(bool), typeof(ImageToggleButton), null);

    public ImageSource CheckedImage
    {
        get { return (ImageSource)base.GetValue(TrueStateImageProperty); }
        set { base.SetValue(TrueStateImageProperty, value); }
    }
    // Using a DependencyProperty as the backing store for TrueStateImage.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty TrueStateImageProperty =
        DependencyProperty.Register("CheckedImage", typeof(ImageSource), typeof(ImageToggleButton), null);

    public ImageSource UncheckedImage
    {
        get { return (ImageSource)base.GetValue(FalseStateImageProperty); }
        set { base.SetValue(FalseStateImageProperty, value); }
    }
    // Using a DependencyProperty as the backing store for FalseStateImage.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty FalseStateImageProperty =
        DependencyProperty.Register("UncheckedImage", typeof(ImageSource), typeof(ImageToggleButton), null);
}

主窗口:

   <ImageToggleButton Checked="{Binding IsPlaying}" CheckedImage="{DynamicResource PauseIcon}" UncheckedImage="{DynamicResource PlayIcon}">
    </ImageToggleButton>

是否可以使用CheckedImage作为默认图像,以便如果我不提供UncheckedImage,那么在选中和未选中状态下都会显示CheckedImage?

您可以为 CheckedImage 属性注册一个PropertyChangedCallback,用于设置另一个属性的值:

public static readonly DependencyProperty TrueStateImageProperty =
    DependencyProperty.Register("CheckedImage", typeof(ImageSource), typeof(ImageToggleButton),
        new PropertyMetadata(new PropertyChangedCallback(OnPropChanged)));
private static void OnPropChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    ImageToggleButton tb = (ImageToggleButton)d;
    if (tb.UncheckedImage == null)
        tb.UncheckedImage = (ImageSource)e.NewValue;
}

顺便说一下,您应该遵循依赖项属性命名约定:https://learn.microsoft.com/en-us/dotnet/framework/wpf/advanced/custom-dependency-properties

相关内容

  • 没有找到相关文章

最新更新