c# - 仅在属性存在时绑定(bind)到属性

标签 c# wpf xaml mvvm

我有一个使用多个 View 模型对象作为其 DataContext 的 WPF 窗口。该窗口有一个控件,该控件绑定(bind)到仅存在于某些 View 模型对象中的属性。如果它存在(并且仅当它存在),我如何绑定(bind)到该属性。

我知道以下问题/答案:MVVM - hiding a control when bound property is not present .这有效,但给了我一个警告。可以在没有警告的情况下完成吗?

谢谢!

一些示例代码:

Xaml:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication1"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="40"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="40"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <ListBox Grid.Row="1" Name="ListView" Margin="25,0,25,0" ItemsSource="{Binding Path=Lst}"
              HorizontalContentAlignment="Center" SelectionChanged="Lst_SelectionChanged">
    </ListBox>
    <local:SubControl Grid.Row="3" x:Name="subControl" DataContext="{Binding Path=SelectedVM}"/>
</Grid>

子控件 Xaml:

<UserControl x:Class="WpfApplication1.SubControl"
         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:WpfApplication1"
         mc:Ignorable="d" 
         d:DesignHeight="200" d:DesignWidth="300">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="40"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="40"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <StackPanel Grid.Row="1" Orientation ="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,5,0,5">
        <TextBlock Margin="5,0,5,0" Text="{Binding Path=OnOffAlways}"/>
        <CheckBox IsChecked="{Binding Path=Always}">
            <TextBlock Text="On/Off"/>
        </CheckBox>
    </StackPanel>
    <StackPanel Grid.Row="3" Orientation ="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,5,0,5">
        <TextBlock Margin="5,0,5,0" Text="{Binding Path=OnOffSometimes}"/>
        <CheckBox IsChecked="{Binding Path=Sometimes}">
            <TextBlock Text="On/Off"/>
        </CheckBox>
    </StackPanel>
</Grid>

主窗口代码隐藏:

    public partial class MainWindow : Window
{
    ViewModel1 vm1;
    ViewModel2 vm2;
    MainViewModel mvm;

    public MainWindow()
    {

        InitializeComponent();

        vm1 = new ViewModel1();
        vm2 = new ViewModel2();
        mvm = new MainViewModel();
        mvm.SelectedVM = vm1;
        DataContext = mvm;
    }

    private void Lst_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        ListBox lstBx = sender as ListBox;

        if (lstBx != null)
        {
            if (lstBx.SelectedItem.Equals("VM 1"))
                mvm.SelectedVM = vm1;
            else if (lstBx.SelectedItem.Equals("VM 2"))
                mvm.SelectedVM = vm2;
        }
    }
}

MainViewModel(MainWindow 的 DataContext):

    public class MainViewModel : INotifyPropertyChanged
{
    ObservableCollection<string> lst;
    ViewModelBase selectedVM;

    public event PropertyChangedEventHandler PropertyChanged;

    public MainViewModel()
    {

        Lst = new ObservableCollection<string>();
        Lst.Add("VM 1");
        Lst.Add("VM 2");
    }

    public ObservableCollection<string> Lst
    {
        get { return lst; }
        set
        {
            lst = value;
            OnPropertyChanged("Lst");
        }
    }


    public ViewModelBase SelectedVM
    {
        get { return selectedVM; }
        set
        {
            if (selectedVM != value)
            {
                selectedVM = value;
                OnPropertyChanged("SelectedVM");
            }
        }
    }
    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }
}

ViewModel1(有时带有属性):

    public class ViewModel1 : ViewModelBase, INotifyPropertyChanged
{
    private bool _always;
    private string _onOffAlways;

    private bool _sometimes;
    private string _onOffSometimes;

    public event PropertyChangedEventHandler PropertyChanged;

    public ViewModel1()
    {
        _always = false;
        _onOffAlways = "Always Off";

        _sometimes = false;
        _onOffSometimes = "Sometimes Off";
    }

    public bool Always
    {
        get { return _always; }
        set
        {
            _always = value;
            if (_always)
                OnOffAlways = "Always On";
            else
                OnOffAlways = "Always Off";
            OnPropertyChanged("Always");
        }
    }

    public string OnOffAlways
    {
        get { return _onOffAlways; }
        set
        {
            _onOffAlways = value;
            OnPropertyChanged("OnOffAlways");
        }
    }

    public bool Sometimes
    {
        get { return _sometimes; }
        set
        {
            _sometimes = value;
            if (_sometimes)
                OnOffSometimes = "Sometimes On";
            else
                OnOffSometimes = "Sometimes Off";
            OnPropertyChanged("Sometimes");
        }
    }

    public string OnOffSometimes
    {
        get { return _onOffSometimes; }
        set
        {
            _onOffSometimes = value;
            OnPropertyChanged("OnOffSometimes");
        }
    }

    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }
}

ViewModel2(没有 Sometimes 属性):

    public class ViewModel2 : ViewModelBase, INotifyPropertyChanged
{
    private bool _always;
    private string _onOffAlways;

    public event PropertyChangedEventHandler PropertyChanged;

    public ViewModel2()
    {
        _always = false;
        _onOffAlways = "Always Off";
    }

    public bool Always
    {
        get { return _always; }
        set
        {
            _always = value;
            if (_always)
                OnOffAlways = "Always On";
            else
                OnOffAlways = "Always Off";
            OnPropertyChanged("Always");
        }
    }

    public string OnOffAlways
    {
        get { return _onOffAlways; }
        set
        {
            _onOffAlways = value;
            OnPropertyChanged("OnOffAlways");
        }
    }

    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }
}

public class AlwaysVisibleConverter : IValueConverter
{
    #region Implementation of IValueConverter

    public object Convert(object value,
                          Type targetType, object parameter, CultureInfo culture)
    {
        return Visibility.Visible;
    }

    public object ConvertBack(object value, Type targetType,
                              object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }

    #endregion
}

最佳答案

有许多不同的方法可以处理您的场景。对于它的值(value),你已经拥有的解决方案对我来说似乎是合理的。您收到的警告(我假设您是在谈论输出到调试控制台的错误消息)相当无害。它确实暗示潜在的性能问题,因为它表明 WPF 正在从意外情况中恢复。但我希望只有当 View 模型发生变化时才会产生成本,这种变化应该不会太频繁。

另一种选择(恕我直言,首选)是仅使用常用的 WPF 数据模板功能。即,为你期望的每个 View 模型定义一个不同的模板,然后让 WPF 根据当前的 View 模型选择合适的模板。这看起来像这样:

<UserControl x:Class="TestSO46736914MissingProperty.UserControl1"
             x:ClassModifier="internal"
             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:l="clr-namespace:TestSO46736914MissingProperty"
             mc:Ignorable="d" 
             Content="{Binding}"
             d:DesignHeight="300" d:DesignWidth="300">
  <UserControl.Resources>
    <DataTemplate DataType="{x:Type l:ViewModel1}">
      <Grid>
        <Grid.RowDefinitions>
          <RowDefinition Height="40"/>
          <RowDefinition Height="Auto"/>
          <RowDefinition Height="40"/>
          <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <StackPanel Grid.Row="1" Orientation ="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,5,0,5">
          <TextBlock Margin="5,0,5,0" Text="{Binding Path=OnOffAlways}"/>
          <CheckBox IsChecked="{Binding Path=Always}">
            <TextBlock Text="On/Off"/>
          </CheckBox>
        </StackPanel>
        <StackPanel Grid.Row="3" Orientation ="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,5,0,5">
          <TextBlock Margin="5,0,5,0" Text="{Binding Path=OnOffSometimes}"/>
          <CheckBox IsChecked="{Binding Path=Sometimes}">
            <TextBlock Text="On/Off"/>
          </CheckBox>
        </StackPanel>
      </Grid>
    </DataTemplate>
    <DataTemplate DataType="{x:Type l:ViewModel2}">
      <Grid>
        <Grid.RowDefinitions>
          <RowDefinition Height="40"/>
          <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <StackPanel Grid.Row="1" Orientation ="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,5,0,5">
          <TextBlock Margin="5,0,5,0" Text="{Binding Path=OnOffAlways}"/>
          <CheckBox IsChecked="{Binding Path=Always}">
            <TextBlock Text="On/Off"/>
          </CheckBox>
        </StackPanel>
      </Grid>
    </DataTemplate>
  </UserControl.Resources>
</UserControl>

即只需设置 Content你的UserControl对象到 View 模型对象本身,以便使用适当的模板来显示控件中的数据。不具有该属性的 View 模型对象的模板不引用该属性,因此不会生成警告。

与上述一样,另一个选项也解决了您对显示的警告的担忧,即创建一个“垫片”(又名“适配器”)对象,该对象在未知 View 模型类型和一致的 View 模型类型之间进行调解 UserControl可以使用。例如:

class ViewModelWrapper : NotifyPropertyChangedBase
{
    private readonly dynamic _viewModel;

    public ViewModelWrapper(object viewModel)
    {
        _viewModel = viewModel;
        HasSometimes = viewModel.GetType().GetProperty("Sometimes") != null;
        _viewModel.PropertyChanged += (PropertyChangedEventHandler)_OnPropertyChanged;
    }

    private void _OnPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        _RaisePropertyChanged(e.PropertyName);
    }

    public bool Always
    {
        get { return _viewModel.Always; }
        set { _viewModel.Always = value; }
    }

    public string OnOffAlways
    {
        get { return _viewModel.OnOffAlways; }
        set { _viewModel.OnOffAlways = value; }
    }

    public bool Sometimes
    {
        get { return HasSometimes ? _viewModel.Sometimes : false; }
        set { if (HasSometimes) _viewModel.Sometimes = value; }
    }

    public string OnOffSometimes
    {
        get { return HasSometimes ? _viewModel.OnOffSometimes : null; }
        set { if (HasSometimes) _viewModel.OnOffSometimes = value; }
    }

    private bool _hasSometimes;
    public bool HasSometimes
    {
        get { return _hasSometimes; }
        private set { _UpdateField(ref _hasSometimes, value); }
    }
}

此对象使用 dynamic C# 中的功能访问已知的属性值,并使用构造反射来确定它是否应该尝试访问 Sometimes (和相关的 OnOffSometimes )属性(通过 dynamic 类型的变量访问该属性,当它不存在时会抛出异常)。

它还实现了 HasSometimes属性,以便 View 可以相应地动态调整自身。最后,它还代理底层 PropertyChanged事件,以及委托(delegate)属性本身。

要使用它,需要一些 UserControl 的代码隐藏需要:

partial class UserControl1 : UserControl, INotifyPropertyChanged
{
    public ViewModelWrapper ViewModelWrapper { get; private set; }

    public UserControl1()
    {
        DataContextChanged += _OnDataContextChanged;
        InitializeComponent();
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void _OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
    {
        ViewModelWrapper = new ViewModelWrapper(DataContext);
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(ViewModelWrapper)));
    }
}

有了这个,XAML 与您最初拥有的大致相同,但样式应用于可选的 StackPanel。具有根据属性是否存在显示或隐藏元素的触发器的元素:

<UserControl x:Class="TestSO46736914MissingProperty.UserControl1"
             x:ClassModifier="internal"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:p="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:l="clr-namespace:TestSO46736914MissingProperty"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
  <Grid DataContext="{Binding ViewModelWrapper, RelativeSource={RelativeSource AncestorType=UserControl}}">
    <Grid.RowDefinitions>
      <RowDefinition Height="40"/>
      <RowDefinition Height="Auto"/>
      <RowDefinition Height="40"/>
      <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <StackPanel Grid.Row="1" Orientation ="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,5,0,5">
      <TextBlock Margin="5,0,5,0" Text="{Binding Path=OnOffAlways}"/>
      <CheckBox IsChecked="{Binding Path=Always}">
        <TextBlock Text="On/Off"/>
      </CheckBox>
    </StackPanel>
    <StackPanel Grid.Row="3" Orientation ="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,5,0,5">
      <StackPanel.Style>
        <p:Style TargetType="StackPanel">
          <p:Style.Triggers>
            <DataTrigger Binding="{Binding HasSometimes}" Value="False">
              <Setter Property="Visibility" Value="Collapsed"/>
            </DataTrigger>
          </p:Style.Triggers>
        </p:Style>
      </StackPanel.Style>
      <TextBlock Margin="5,0,5,0" Text="{Binding Path=OnOffSometimes}"/>
      <CheckBox IsChecked="{Binding Path=Sometimes}">
        <TextBlock Text="On/Off"/>
      </CheckBox>
    </StackPanel>
  </Grid>
</UserControl>

注意顶层Grid元素的 DataContext设置为 UserControlViewModelWrapper属性,以便包含的元素使用该对象而不是父代码分配的 View 模型。

(您可以忽略 p: XML 命名空间……那只是因为 Stack Overflow 的 XAML 格式会被使用默认 XML 命名空间的 <Style/> 元素混淆。)

虽然我通常更喜欢基于模板的方法,因为它是惯用的且本质上更简单的方法,但这种基于包装器的方法确实有一些优点:

  • 可用于UserControl对象在与声明 View 模型类型的程序集中不同的程序集中声明,并且前者不能引用后一个程序集。
  • 它消除了基于模板的方法所需的冗余。 IE。这种方法不必复制/粘贴模板的共享元素,而是对整个 View 使用单个 XAML 结构,并根据需要显示或隐藏该 View 的元素。

为了完整起见,这里是 NotifyPropertyChangedBase ViewModelWrapper 使用的类上面的类:

class NotifyPropertyChangedBase : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected void _UpdateField<T>(ref T field, T newValue,
        Action<T> onChangedCallback = null,
        [CallerMemberName] string propertyName = null)
    {
        if (EqualityComparer<T>.Default.Equals(field, newValue))
        {
            return;
        }

        T oldValue = field;

        field = newValue;
        onChangedCallback?.Invoke(oldValue);
        _RaisePropertyChanged(propertyName);
    }

    protected void _RaisePropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

就其值(value)而言,我更喜欢这种方法来重新实现 INotifyPropertyChanged每个模型对象中的接口(interface)。代码更简单,更易于编写,更易于阅读,并且不易出错。

关于c# - 仅在属性存在时绑定(bind)到属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46736914/

相关文章:

c# - 当标签不为空时隐藏按钮

c# - 尝试添加命令行支持后无法使用断点

c# - 为什么我的类(class)突然有一个 'designer' ?

c# - 可为空的短整型和数组

c# - 在 powershell 中获取 wpf 应用程序的退出代码

c# - WPF 自定义复合用户控件

c# - 未调用 WPF IValueConverter.ConvertBack

c# - 使用 StackExchange.Redis 的循环发布/订阅

c# - 仅显示index.cshtml中的x个项目

wpf - DevExpress GridControl 列自动宽度