wpf – 绑定UserControl依赖属性和MVVM

前端之家收集整理的这篇文章主要介绍了wpf – 绑定UserControl依赖属性和MVVM前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个包含UserControl的MainWindow,它们都是用MVVM模式实现的.
MainWindowVM具有我想要绑定到UserControl1VM中的属性属性.但这不起作用.

这里是一些代码(viewmodels使用某种mvvm框架,在viewmodelBase类中实现INotifyPropertyChanged,但希望没问题):

MainWindow.xaml:

  1. <Window x:Class="DPandMVVM.MainWindow"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:local="clr-namespace:DPandMVVM"
  5. Title="MainWindow" Height="300" Width="300">
  6. <Grid>
  7. <local:UserControl1 TextInControl="{Binding Text}" />
  8. </Grid>
  9. </Window>

CodeBehind MainWindow.xaml.cs:

  1. using System.Windows;
  2. namespace DPandMVVM
  3. {
  4. public partial class MainWindow : Window
  5. {
  6. public MainWindow()
  7. {
  8. InitializeComponent();
  9. DataContext = new MainWindowVM();
  10. }
  11. }
  12. }

MainWindow-viewmodel MainWindowVM.cs:

  1. namespace DPandMVVM
  2. {
  3. public class MainWindowVM : viewmodelBase
  4. {
  5. private string _text;
  6. public string Text { get { return _text; } }
  7.  
  8. public MainWindowVM()
  9. {
  10. _text = "Text from MainWindowVM";
  11. }
  12. }
  13. }

在这里UserControl1.xaml:

  1. <UserControl x:Class="DPandMVVM.UserControl1"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  5. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  6. mc:Ignorable="d"
  7. d:DesignHeight="300" d:DesignWidth="300">
  8. <Grid>
  9. <TextBlock Text="{Binding TextInTextBlock}" />
  10. </Grid>
  11. </UserControl>

Codebehind UserControl1.xaml.cs:

  1. using System.Windows.Controls;
  2. namespace DPandMVVM
  3. {
  4. /// <summary>
  5. /// Interaction logic for UserControl1.xaml
  6. /// </summary>
  7. public partial class UserControl1 : UserControl
  8. {
  9. public UserControl1()
  10. {
  11. InitializeComponent();
  12. DataContext = new UserControl1VM();
  13. }
  14. }
  15. }

viewmodel UserControl1VM.cs:

  1. using System.Windows;
  2. namespace DPandMVVM
  3. {
  4. public class UserControl1VM : DependencyObject
  5. {
  6. public UserControl1VM()
  7. {
  8. TextInControl = "TextfromUserControl1VM";
  9. }
  10.  
  11. public string TextInControl
  12. {
  13. get { return (string)GetValue(TextInControlProperty); }
  14. set { SetValue(TextInControlProperty,value); }
  15. }
  16.  
  17. public static readonly DependencyProperty TextInControlProperty =
  18. DependencyProperty.Register("TextInControl",typeof(string),typeof(UserControl1VM));
  19. }
  20. }

使用此星座,无法在MainWindow.xaml中找到DP.

我究竟做错了什么?

首先,如果要从外部绑定它,则需要在UserControl1中声明DependencyProperty TextInControl.

在UserControl1中移动DP的声明.

  1. public partial class UserControl1 : UserControl
  2. {
  3. public UserControl1()
  4. {
  5. InitializeComponent();
  6. }
  7.  
  8. public string TextInControl
  9. {
  10. get { return (string)GetValue(TextInControlProperty); }
  11. set { SetValue(TextInControlProperty,value); }
  12. }
  13.  
  14. public static readonly DependencyProperty TextInControlProperty =
  15. DependencyProperty.Register("TextInControl",typeof(UserControl1));
  16. }

其次,你将UserControl的DataContext外部设置为UserControl1VM,

  1. public UserControl1()
  2. {
  3. InitializeComponent();
  4. DataContext = new UserControl1VM(); <-- HERE (Remove this)
  5. }

所以WPF绑定引擎在UserControl1VM中寻找属性Text而不是MainWindowVM.删除设置DataContext并将UserControl1的XAML更新为:

  1. <UserControl x:Class="DPandMVVM.UserControl1"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  5. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  6. mc:Ignorable="d"
  7. d:DesignHeight="300" d:DesignWidth="300"
  8. x:Name="userControl1">
  9. <Grid>
  10. <TextBlock Text="{Binding TextInTextBlock,ElementName=userControl1}" />
  11. </Grid>
  12. </UserControl>

通过在UserControl上设置x:Name,使用ElementName绑定DP.

UPDATE

如果您希望UserControl的viewmodel完好无损,则必须更新MainWindow中的绑定.显式告诉WPF绑定引擎在MainWindow的DataContext中使用ElementName查找属性,如下所示:

  1. <local:UserControl1 TextInControl="{Binding DataContext.Text,ElementName=mainWindow}" />

为此,您需要在窗口根级别设置x:Name =“mainWindow”.

猜你在找的设计模式相关文章