C# 自定义UserControl的WPF数据绑定问题
我在UserControl的DependencyProperty和TextBlock的DependencyProperty上使用数据绑定。我将这些绑定到属性TestValue。 首先,我用TestValue=“TestValue set First time!”初始化TestValue有效同时更新用户控件和文本块。C# 自定义UserControl的WPF数据绑定问题,c#,wpf,data-binding,C#,Wpf,Data Binding,我在UserControl的DependencyProperty和TextBlock的DependencyProperty上使用数据绑定。我将这些绑定到属性TestValue。 首先,我用TestValue=“TestValue set First time!”初始化TestValue有效同时更新用户控件和文本块。 然后,通过Dispatcher,我每秒向TestValue写入一个随机值,而这个不起作用。TextBlock会更新,但UserControl不会更新 以下是我的主窗口代码: //Ma
然后,通过Dispatcher,我每秒向TestValue写入一个随机值,而这个不起作用。TextBlock会更新,但UserControl不会更新 以下是我的主窗口代码:
//MainWindow.cs
public partial class MainWindow : Window
{
private MainWindowViewModel viewModel;
public MainWindow()
{
InitializeComponent();
this.DataContext = this.viewModel = new MainWindowViewModel()
{
TestValue = "TestValue set first time!",
};
}
}
//MainWindow.xaml
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local ="clr-namespace:RhinoTouchUIwpf;assembly="
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="RhinoTouchUIwpf.MainWindow"
SizeToContent="WidthAndHeight">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
</Grid.ColumnDefinitions>
<local:UserControl1 OperationName="{Binding TestValue, Mode=OneWay}" Grid.Row="0"/>
<TextBlock Text="{Binding TestValue, Mode=OneWay}" FontSize="15" Grid.Row="1" />
</Grid>
TextBlock的文本每秒都会更改,UserControl1的文本不会更改。我做错了什么?您遇到的问题是
属性ChangedCallback中的这行代码:
((UserControl1)sender).OperationName = (String)e.NewValue;
这里的问题是,事件处理程序将通知您对同一属性的新更改。当您输入此功能时,它来自:
set { this.SetValue(MyDependencyProperty, value); }
有效地进入无限循环(依赖属性系统检测并停止对该函数的进一步调用)。去掉那条线就能解决你的问题
另一方面,如果您在XAML中定义了属性changedCallback
,则可以完全取消该属性:
// On your UserControl element, add a x:Name="uc"
<TextBlock x:Name="NumberLabel" FontSize="20"
Text="{Binding OperationName, ElementName=uc, FallbackValue=0}"
Grid.Column="0" Grid.Row="1" HorizontalAlignment="Center"
VerticalAlignment="Center"/>
//在UserControl元素上添加一个x:Name=“uc”
这是完整的UserControl1.xaml
<UserControl x:Class="WpfApplication1.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" x:Name="uc"
d:DesignHeight="300" d:DesignWidth="300">
<Border BorderThickness="1" BorderBrush="Blue" x:Name="ControlBorder">
<Grid Background="AliceBlue">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock x:Name="NumberLabel" FontSize="20"
Text="{Binding OperationName, ElementName=uc, FallbackValue=0}"
Grid.Column="0" Grid.Row="1" HorizontalAlignment="Center"
VerticalAlignment="Center"/>
</Grid>
</Border>
</UserControl>
// On your UserControl element, add a x:Name="uc"
<TextBlock x:Name="NumberLabel" FontSize="20"
Text="{Binding OperationName, ElementName=uc, FallbackValue=0}"
Grid.Column="0" Grid.Row="1" HorizontalAlignment="Center"
VerticalAlignment="Center"/>
<UserControl x:Class="WpfApplication1.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" x:Name="uc"
d:DesignHeight="300" d:DesignWidth="300">
<Border BorderThickness="1" BorderBrush="Blue" x:Name="ControlBorder">
<Grid Background="AliceBlue">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock x:Name="NumberLabel" FontSize="20"
Text="{Binding OperationName, ElementName=uc, FallbackValue=0}"
Grid.Column="0" Grid.Row="1" HorizontalAlignment="Center"
VerticalAlignment="Center"/>
</Grid>
</Border>
</UserControl>