gpt4 book ai didi

wpf - 将 wpf 用户控件绑定(bind)到父属性

转载 作者:行者123 更新时间:2023-12-02 09:20:55 25 4
gpt4 key购买 nike

我有一个简单的用户控件,其中包含一个图像,我想根据父级(可能是另一个 UC 或 Window)中的属性更改其来源。 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 中的状态绑定(bind)到窗口的 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>

然而,我得到的是一个错误,指出在“对象”“MainWindow”上找不到“State”属性,因此它似乎是按字面意思采用 UC 中的绑定(bind)“State”,而不是将其分配给窗口的 DualState 属性。有人可以了解我做错了什么吗?

如果我通过代码或 XAML(作为 bool 值)在 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); }
}

它的数据类型是否需要绑定(bind)或其他类型才能使其工作?

最佳答案

DataTrigger 的 DataContext 设置为窗口,这就是它查看“状态”窗口的原因。您只需要告诉绑定(bind) 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>

主窗口.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();
}
}
}

关于wpf - 将 wpf 用户控件绑定(bind)到父属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42659813/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com