我仔细观察了很久,却被困住了。我试图通过 Window 的绑定将参数从 Window 传递到 UserControl1。
在 MainWindow 中,UserControl1 被包含两次,一次通过 MyValue 上的绑定传递参数 MyCustom,再次使用文字。传递绑定对 UserControl1 没有影响。 MyCustom 依赖属性未更改。按照字面意思,它会按预期工作。
我很困惑。我已将示例复制到https://stackoverflow.com/a/21718694/468523 https://stackoverflow.com/a/21718694/468523但没有喜悦。我一定缺少一些简单的东西。
对我复制的所有代码感到抱歉,但问题往往在于细节..
主窗口.xaml
<Window x:Class="MyParamaterizedTest3.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:MyParamaterizedTest3"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Grid HorizontalAlignment="Center" VerticalAlignment="Center">
<StackPanel>
<Rectangle Height="20"/>
<local:UserControl1 MyCustom="{Binding MyValue, UpdateSourceTrigger=PropertyChanged}"/>
<Rectangle Height="20"/>
<local:UserControl1 MyCustom="Literal Stuff"/>
<Rectangle Height="20"/>
<StackPanel Orientation="Horizontal">
<TextBlock Text="MainWindow: "/>
<TextBlock Text="{Binding MyValue, UpdateSourceTrigger=PropertyChanged}"/>
</StackPanel>
</StackPanel>
</Grid>
</Window>
MainWindow.xaml.cs
namespace MyParamaterizedTest3
{
public partial class MainWindow : INotifyPropertyChanged
{
public MainWindow()
{
InitializeComponent();
}
public string MyValue { get => _myValue; set => SetField(ref _myValue, value); }
private string _myValue= "First things first";
public event PropertyChangedEventHandler PropertyChanged;
protected bool SetField<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
{
if (EqualityComparer<T>.Default.Equals(field, value)) { return false; }
field = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
return true;
}
}
}
UserControl1.xaml(在下面更正)
<UserControl x:Class="MyParamaterizedTest3.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:MyParamaterizedTest3"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
>
<Grid HorizontalAlignment="Center" VerticalAlignment="Center">
<Border BorderThickness="3" BorderBrush="Black">
<StackPanel>
<TextBlock Text="{Binding MyCustom, UpdateSourceTrigger=PropertyChanged, FallbackValue=mycustom}"></TextBlock>
</StackPanel>
</Border>
</Grid>
</UserControl>
UserControl1.xaml.cs(在下面更正)
namespace MyParamaterizedTest3
{
public partial class UserControl1 : INotifyPropertyChanged
{
public UserControl1()
{
InitializeComponent();
}
public static readonly DependencyProperty MyCustomProperty =
DependencyProperty.Register("MyCustom", typeof(string), typeof(UserControl1));
public string MyCustom
{
get
{
return this.GetValue(MyCustomProperty) as string;
}
set
{
this.SetValue(MyCustomProperty, value);
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected bool SetField<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
{
if (EqualityComparer<T>.Default.Equals(field, value)) { return false; }
field = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
return true;
}
}
}
更正了 UserControl1.xaml(根据 Ed Plunkett)
<UserControl x:Class="MyParamaterizedTest3.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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
>
<Grid HorizontalAlignment="Center" VerticalAlignment="Center">
<Border BorderThickness="3" BorderBrush="Black">
<StackPanel>
<TextBlock Text="{Binding MyCustom, RelativeSource={RelativeSource AncestorType=UserControl}, FallbackValue=mycustom}"></TextBlock>
</StackPanel>
</Border>
</Grid>
</UserControl>
更正了 UserControl1.xaml.cs(根据 Ed Plunkett)
<UserControl x:Class="MyParamaterizedTest3.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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
>
<Grid HorizontalAlignment="Center" VerticalAlignment="Center">
<Border BorderThickness="3" BorderBrush="Black">
<StackPanel>
<TextBlock Text="{Binding MyCustom, RelativeSource={RelativeSource AncestorType=UserControl}, FallbackValue=mycustom}"></TextBlock>
</StackPanel>
</Border>
</Grid>
</UserControl>