gpt4 book ai didi

wpf - 绑定(bind) WPF UserControl 以查看模型和代码

转载 作者:行者123 更新时间:2023-12-03 10:16:09 25 4
gpt4 key购买 nike

我试图了解连接自定义控件以使用依赖属性和 View 模型的最佳方式。实现依赖属性以公开可在 XAML 中用于初始化 View 模型中的属性的属性。例如,在自定义控件的代码中,我可以定义以下依赖属性:

public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.Register(
"MyProperty", typeof(string), typeof(MyControl), new PropertyMetadata(null));

public string MyProperty
{
get { return (string)GetValue(MyPropertyProperty); }
set { SetValue(MyPropertyProperty, value); }
}

View 模型定义为
public class MyControlViewModel : INotifyPropertyChanged
{
public MyControlViewModel()
{
_myProperty = "Default View Model string";
}

private string _myProperty;

public string MyProperty
{
get
{
return _myProperty;
}
set
{
_myProperty = value;
OnPropertyChanged("MyProperty");
}
}

public event PropertyChangedEventHandler PropertyChanged;

protected virtual void OnPropertyChanged(string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));

}
}

并且自定义控件绑定(bind)到 View Model MyProperty 如下
<UserControl x:Class="MyProject.MyControlView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyProject">

<UserControl.DataContext>
<local:MyControlViewModel/>
</UserControl.DataContext>

<Grid>
<TextBlock Text="{Binding MyProperty}"/>
</Grid>

</UserControl>

现在,由于我在自定义控件的代码中定义了依赖属性 MyProperty,我希望能够使用它来初始化 ViewModel 中的 MyProperty。所以像这样
 <Window x:Class="MyProject.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyProject"
Title="MainWindow" Height="350" Width="525">
<Grid>
<local:MyControlView MyProperty="This was set in XAML"/>
</Grid>
</Window>

运行上述将显示在 View 模型的构造函数中设置的字符串“默认 View 模型字符串”。如何连接依赖属性值,以便正确初始化 View 模型中的字符串?即它应该显示“这是在 XAML 中设置的”。

更新

我可以在后面的代码中设置属性更改回调并在 View 模型中设置值,即
public static readonly DependencyProperty MyPropertyProperty =
DependencyProperty.Register("MyProperty", typeof(string), typeof(MyControlView), new PropertyMetadata("Default", OnMyPropertyChanged));

private static void OnMyPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var view = d as MyControlView;
if (view != null)
{
var viewModel = view.DataContext as MyControlViewModel;
if (viewModel != null)
{
viewModel.MyProperty = e.NewValue as string;
}
}
}

这是正确的,还是有气味?

最佳答案

您已经创建了很好的依赖属性。但是,您已经通过实例化您的 View 模型并将其属性使用到后面的代码中来实现紧密耦合。它的错误方式。

您应该从“Control ”继承您的类,并为绑定(bind)值创建一个 DP如下 custom control class :

public static readonly DependencyProperty myValueProperty = DependencyProperty.Register(
"MyProperty", typeof(object), typeof(FieldControl), new FrameworkPropertyMetadata(null, myValueChanged));

然后,
private static void myValueChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{
var fc = dependencyObject as FieldControl;
if (fc != null)
{
fc.SetValue(BindingExpression1Key, fc.GetBindingExpression(ValueProperty));
fc.RaiseValueChangedEvent(dependencyPropertyChangedEventArgs);
}
}

现在,使用它并绑定(bind)您的属性,如下所示:
 <local:MyControlView MyProperty="{Binding MyProperty, Mode=TwoWay}"/>

关于wpf - 绑定(bind) WPF UserControl 以查看模型和代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31047284/

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