wpf - MVVM 中具有依赖属性的用户控件

标签 wpf xaml mvvm user-controls

一个新手问题。谷歌搜索了几个小时后,我仍然无法得到这个(标准)情况的明确答案。

我有一个 UserControl_1,它将由一个自定义控件和一个 WPF 控件组成。用户控件将用于其他 WPF 用户控件。

最终,我想在主用户控件的 XAML 中执行此操作:

  XAML
      <i:UserControl_1  TargetDP = "{Binding SourceProperty, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" }" />

其中 SourceProperty 绑定(bind)到 Main 用户控件的 DataContext,TargetDP 是 UserControl_1 中的依赖属性。然后 UserControl_1 将此 TargetDP 向下/向上传递给其包含的控件。

那么我应该把 UserControl 的 DependencyProperty 放在哪里呢?

似乎它应该放在代码隐藏中,但我真的很想使用 MVVM 来做到这一点。

UserControl_1 应该有自己的 ViewModel 与主用户控件分开,但信息应该能够通过 UserControl XAML 绑定(bind)在主视图模型和用户控件 View 模型之间传递(反之亦然)。

感谢您对此的任何帮助或澄清。 (对不起,如果这看起来是重复的,但其他文章似乎都没有指定在哪里创建用户控件的新依赖属性或如何实现这一点)。

最佳答案

这是如何做到的(如果我理解正确的话!)。你有一个用户控件,叫它MyControl .创建 MyControlView

<UserControl x:Class="Project.Controls.MyControlView"
             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">
    <Grid HorizontalAlignment="Stretch"
            VerticalAlignment="Stretch">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <TextBlock Grid.Column="0" 
                   Text="{Binding LabelText}" 
                   VerticalAlignment="Center"
                   HorizontalAlignment="Right"
                   Margin="5,0"/>
        <ComboBox Grid.Row="1" 
                  ItemsSource="{Binding ComboBoxItems}"
                  SelectedItem="{Binding SelectedItem}"
                  HorizontalAlignment="Stretch"
                  Margin="0,0,5,0"/>
    </Grid>
</UserControl>

现在 View 模型将是
public class MyControlViewModel
{
    // Add the public properties for LabelText, ComboBoxItems and SelectedItem
    // and any other logic you require.
}

现在对于您感到困惑的那一点。如果我想在另一个 View 中使用这个控件,调用它BigView我可以
<Window x:Class="NameSpace.BigView"
        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:Controls="clr-namespace:Project.Controls"> // Important reference to your control namespace.
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Controls:MyControlView Grid.Row="0" Margin="0,5" 
                                DataContext="{Binding MyControlA}"/>
        <Controls:MyControlView Grid.Row="1" Margin="0,5" 
                                DataContext="{Binding MyControlB}"/>
    </Grid>
</Window>

现在在 View 模型 BigViewModel你将有两个属性
public RangeSliderControlViewModel MyControlA { get; set; }
public RangeSliderControlViewModel MyControlB { get; set; }

因此,您可以通过 MyControlA.LabelText = "Some Text" 访问每个控件的属性。等。请注意,在 BigViewModel MyControlA/MyControlB不需要执行INotifyPropertyChanged但是,您必须控制的属性才能使更新冒泡并更新 BigView .

我希望这有帮助。

关于wpf - MVVM 中具有依赖属性的用户控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27309908/

相关文章:

c# - 静态 ViewModel 与实例化 ViewModel

c# - 从子控件获取父面板的 "name"

c# - 生成随机数并将其放入 TextBlock

c# - 在 TabControl 中设置 DataContext

WPF、Project White 和 Infragistics

c# - 包含内容的 WPF 新窗口

XAML标记列表引用

wpf - Prism2/MVVM 从 ViewModel 关闭 View

c# - 在按钮上打开一个新窗口单击 WPF MVVM

c# - WPF donut 进度条