wpf - 如何正确绑定(bind)到 MVVM 框架中用户控件的依赖属性

标签 wpf mvvm data-binding user-controls dependency-properties

我一直找不到一个干净、简单的示例来说明如何使用具有 DependencyProperty 的 WPF 正确实现用户控件。在 MVVM 框架内。每当我为用户控件分配 DataContext 时,下面的代码都会失败.

我在尝试着:

  • 设置DependencyProperty来自调用 ItemsControl 和
  • 使 DependencyProperty 的值可用于被调用用户控件的 ViewModel。

  • 我还有很多东西要学,真诚地感谢任何帮助。

    这是ItemsControl在调用 InkStringView 的最顶层用户控件中带有 DependencyProperty 的用户控件TextInControl (来自另一个问题的示例)。

    <ItemsControl ItemsSource="{Binding Strings}" x:Name="self" >
    
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel HorizontalAlignment="Left" VerticalAlignment="Top" Orientation="Vertical" />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
    
    
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <DataTemplate.Resources>
                    <Style TargetType="v:InkStringView">
                        <Setter Property="FontSize" Value="25"/>
                        <Setter Property="HorizontalAlignment" Value="Left"/>
                    </Style>
                </DataTemplate.Resources>
    
                <v:InkStringView TextInControl="{Binding text, ElementName=self}"  />
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
    

    这里是 InkStringView带有 DependencyProperty 的用户控件.

    XAML:

    <UserControl x:Class="Nova5.UI.Views.Ink.InkStringView"
             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" 
             x:Name="mainInkStringView"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition/>   
                <RowDefinition/>
            </Grid.RowDefinitions>
    
            <TextBlock Grid.Row="0" Text="{Binding TextInControl, ElementName=mainInkStringView}" />
            <TextBlock Grid.Row="1" Text="I am row 1" />
        </Grid>
    </UserControl>
    

    代码隐藏文件:

    namespace Nova5.UI.Views.Ink
    {
        public partial class InkStringView : UserControl
        {
            public InkStringView()
            {
                InitializeComponent();
                this.DataContext = new InkStringViewModel();   <--THIS PREVENTS CORRECT BINDING, WHAT
            }                                                   --ELSE TO DO?????
    
            public String TextInControl
            {
                get { return (String)GetValue(TextInControlProperty); }
                set { SetValue(TextInControlProperty, value); }
            }
    
            public static readonly DependencyProperty TextInControlProperty =
                DependencyProperty.Register("TextInControl", typeof(String), typeof(InkStringView));
    
        }
    }
    

    最佳答案

    您快到了。问题是您正在为您的 UserControl 创建一个 ViewModel。这是一种气味。

    从外部看,UserControls 的外观和行为应该与任何其他控件一样。您正确地在控件上公开了属性,并将内部控件绑定(bind)到这些属性。这都是正确的。

    您失败的地方是尝试为所有内容创建 ViewModel。所以放弃那个愚蠢的 InkStringViewModel 并让任何使用该控件的人将他们的 View 模型绑定(bind)到它。

    如果您想问“ View 模型中的逻辑呢?如果我摆脱它,我将不得不将代码放在代码隐藏中!”我回答,“它是业务逻辑吗?无论如何,它不应该嵌入到您的 UserControl 中。而且 MVVM != 没有代码隐藏。为您的 UI 逻辑使用代码隐藏。这是它所属的地方。”

    关于wpf - 如何正确绑定(bind)到 MVVM 框架中用户控件的依赖属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25672037/

    相关文章:

    android - 带有 ViewModel 和 Lifecycleowner 的应用程序小部件

    c# - 使用 View 模型值的依赖属性

    java - 无需 Spring MVC 的自动数据绑定(bind) Java 对象

    wpf - 如何绑定(bind)图片源?

    wpf - 有没有一种简单的方法可以让 ListView 自动滚动到最近添加的项目,而无需在后面的代码中编写任何代码?

    xaml - xamarin 表格 : binding HorizontalTextAlignment XAML property to viewmodel property

    android - DataBinding Android,自定义 setter ,不起作用?

    c# - 功能区 ApplicationMenu AuxilaryPane Size

    .net - 强制初始化 HwndHost

    c# - WinForms DataGridView - 数据绑定(bind)到具有列表属性(列数可变)的对象