将WPF用户控件绑定到父属性

3

我有一个简单的用户控件,包含一张图片,我想根据父级(可以是另一个UC或窗口)中的属性来更改其源。简化版本的UC如下所示:

<UserControl x:Class="Test.Controls.DualStateButton" ... x:Name="root">
    <Grid>
        <Image Height="{Binding Height, ElementName=root}" Stretch="Fill" Width="{Binding Width, ElementName=root}">
            <Image.Style>
                <Style TargetType="{x:Type Image}">
                    <Setter Property="Source" Value="{Binding ImageOff, ElementName=root}"/>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding State}" Value="True">
                            <Setter Property="Source" Value="{Binding ImageOn, ElementName=root}"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Image.Style>
        </Image>
    </Grid>
</UserControl>

Height,Width,ImageOff,ImageOn和State都是UC上的依赖属性。UC没有设置DataContext,因此应该继承父级。我想做的是将UC中的State绑定到窗口的DualState属性。

<Window x:Class="Test.MainWindow" DataContext="{Binding RelativeSource={RelativeSource Self}}">
...
    <Grid>
        <local:DualStateButton State="{Binding DualState}" Height="100" ImageOff="{StaticResource ButtonUp}" ImageOn="{StaticResource ButtonDown}" Width="100"/>
    </Grid>
</Window>

然而,我得到的错误是“在 'object' ''MainWindow' 上未找到 'State' 属性”,因此它似乎字面上采用了 UC 中的绑定“State”,而没有将其分配给窗口的 DualState 属性。有人能否对我错在哪里提供一些见解呢?
如果我通过代码或 XAML(作为布尔值)设置 UC 上的 State 属性,它就可以正常工作。 State DP 的定义如下。
public static readonly DependencyProperty StateProperty =
    DependencyProperty.Register("State", typeof(bool), typeof(DualStateButton),
    new PropertyMetadata(false));

public bool State
{
    get { return (bool)GetValue(StateProperty); }
    set { SetValue(StateProperty, value); }
}

这将需要数据类型进行绑定或其他操作才能正常运行吗?

1
双状态属性在哪里?DualState是Window的属性吗?还是它是一些其他viewmodel的属性? - loopedcode
查找 FindAncestor - Mafii
是的,DualState是窗口的一个属性。由于错误提示说“在'object''MainWindow'上找不到'State'属性”,DataContext是正确的(即它正在查看窗口)。问题在于它正在寻找'State'而不是'DualState'。 - user7134019
当您绑定到State时,应该如何查找DualState呢? - mm8
1个回答

7
数据触发器的DataContext设置为窗口,这就是它查找“State”属性的原因。你只需要告诉绑定器State在用户控件上即可。尝试使用以下代码:
<DataTrigger Binding="{Binding Path=State, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}" Value="True">

这里是一个完整的例子:
MainWindow.xaml
<Window x:Class="WpfApplication89.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication89"
        mc:Ignorable="d"
        DataContext="{Binding RelativeSource={RelativeSource Self}}"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel>
        <local:UserControl1 State="{Binding Path=DualState}" />
        <CheckBox Content="DualState" IsChecked="{Binding DualState}" />
    </StackPanel>
</Window>

MainWindow.xaml.cs

using System.Windows;

namespace WpfApplication89
{
    public partial class MainWindow : Window
    {
        public static readonly DependencyProperty DualStateProperty = DependencyProperty.Register("DualState", typeof(bool), typeof(MainWindow), new PropertyMetadata(false));

        public bool DualState
        {
            get { return (bool)GetValue(DualStateProperty); }
            set { SetValue(DualStateProperty, value); }
        }

        public MainWindow()
        {
            InitializeComponent();
        }
    }
}

UserControl1.xaml

<UserControl x:Class="WpfApplication89.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:WpfApplication89"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <TextBlock Text="User Control 1">
            <TextBlock.Style>
                <Style TargetType="TextBlock">
                    <Setter Property="Background" Value="Beige" />
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Path=State, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}" Value="true">
                            <Setter Property="Background" Value="Red" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </TextBlock.Style>
        </TextBlock>
    </Grid>
</UserControl>

UserControl1.xaml.cs

using System.Windows;
using System.Windows.Controls;

namespace WpfApplication89
{
    public partial class UserControl1 : UserControl
    {
        public static readonly DependencyProperty StateProperty = DependencyProperty.Register("State", typeof(bool), typeof(UserControl1), new PropertyMetadata(false));

        public bool State
        {
            get { return (bool)GetValue(StateProperty); }
            set { SetValue(StateProperty, value); }
        }

        public UserControl1()
        {
            InitializeComponent();
        }
    }
}

MainWindow.xaml.cs(实现INotifyPropertyChanged接口的版本)
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;

namespace WpfApplication89
{
    public partial class MainWindow : Window, INotifyPropertyChanged
    {

        #region INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;
        protected bool SetProperty<T>(ref T field, T value, [CallerMemberName]string name = null)
        {
            if (Equals(field, value))
            {
                return false;
            }
            field = value;
            this.OnPropertyChanged(name);
            return true;
        }
        protected void OnPropertyChanged([CallerMemberName]string name = null)
        {
            var handler = this.PropertyChanged;
            handler?.Invoke(this, new PropertyChangedEventArgs(name));
        }
        #endregion

        #region Property bool DualState
        private bool _DualState;
        public bool DualState { get { return _DualState; } set { SetProperty(ref _DualState, value); } }
        #endregion


        public MainWindow()
        {
            InitializeComponent();
        }
    }
}

问题不在数据上下文,因为我希望它查看窗口。问题在于路径。它字面上使用“State”,而不是使用状态依赖属性的值,该值应为“DualState”。 - user7134019
我并不是想暗示问题出在数据上下文。你想绑定到用户控件的State属性,对吗?如果是这样,请尝试这个答案。 - J.H.
我仍然得到相同的错误。实际上,我想要绑定窗口的DualState属性。如果我将数据触发器设置如下,则可以正常工作,但我不想硬编码它,而是将属性名称传递给UC。<DataTrigger Binding="{Binding DualState}" Value="True"> - user7134019
请检查更新后的答案。UserControl.State绑定到MainWindow.xaml中的DualState。在UserControl1.xaml中,DataTrigger绑定到UserControl上的State。 - J.H.
DualState不一定要是DependencyProperty。我将在MainWindow.xaml.cs中编辑答案,使用INotifyPropertyChanged版本。你之前的DualState属性是什么样子的呢?它实现了INotifyPropertyChanged吗? - J.H.
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接