[英]WPF DataBinding Problems with custom UserControl
我在UserControl的DependencyProperty和TextBlock的DependencyProperty上使用DataBinding。 我將那些綁定到屬性TestValue。 首先,我使用TestValue =“第一次設置TestValue來初始化TestValue!”; 這樣可行! UserControl和TextBlock均得到更新。
然后,通過DispatcherTimer,我每秒將一個隨機值寫入TestValue,這不起作用 。 TextBlock得到更新,但UserControl沒有更新。
這是我的MainWindow代碼:
//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>
這是UserControl的代碼:
//UserControl1.cs
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
public static DependencyProperty MyDependencyProperty =DependencyProperty.Register("OperationName", typeof(String), typeof(UserControl1), new UIPropertyMetadata(null, MyDependencyPropertyChangedHandler));
private static void MyDependencyPropertyChangedHandler(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
((UserControl1)sender).OperationName = (String)e.NewValue;
((UserControl1)sender).NumberLabel.Text = (String)e.NewValue;
}
public String OperationName
{
get { return (string)(this.GetValue(MyDependencyProperty)); }
set { this.SetValue(MyDependencyProperty, value); }
}
}
//UserControl1.xaml
<UserControl
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:RhinoTouchUIwpf" x:Class="RhinoTouchUIwpf.UserControl1"
mc:Ignorable="d">
<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="0" Grid.Column="0" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</Border>
這是MainWindowViewModel的代碼,我的屬性由DispatcherTimer每秒更改一次。
public class MainWindowViewModel : ViewModelBase
{
DispatcherTimer dispatcherTimer;
public MainWindowViewModel()
{
dispatcherTimer = new DispatcherTimer();
dispatcherTimer.Tick += Dispatcher_Tick;
dispatcherTimer.Interval = TimeSpan.FromSeconds(1);
dispatcherTimer.IsEnabled = true;
}
void Dispatcher_Tick(object sender, EventArgs e)
{
this.TestValue = "" + DateTime.Now.Millisecond;
}
private String _TestValue;
public String TestValue
{
get { return _TestValue; }
set
{
_TestValue = value;
OnPropertyChanged("TestValue");
}
}
private static readonly MainWindowViewModel NullInstance = null;
}
public abstract class ViewModelBase : System.ComponentModel.INotifyPropertyChanged
{
#region INotifyPropertyChanged Members
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this,
new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
#endregion
}
TextBlock的文本每秒更改一次,而UserControl1的文本則不更改。
我究竟做錯了什么?
您遇到的問題是PropertyChangedCallback
以下代碼行:
((UserControl1)sender).OperationName = (String)e.NewValue;
這里的問題是事件處理程序將通知您對該屬性的新更改。 當您輸入此功能時,它來自:
set { this.SetValue(MyDependencyProperty, value); }
有效地進入無限循環(依賴屬性系統檢測到並停止對該函數的進一步調用)。 刪除該行將解決您的問題。
另一方面,如果您在XAML中定義它,則可以完全擺脫PropertyChangedCallback
:
// 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"/>
這是完整的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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.