仅当属性存在时才绑定到属性

PT2*_*550 6 c# wpf xaml mvvm

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

我知道以下问题/答案:MVVM - 当绑定属性不存在时隐藏控件。这有效,但给了我一个警告。可以在没有警告的情况下完成吗?

谢谢!

一些示例代码:

Xml:

<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>
Run Code Online (Sandbox Code Playgroud)

子控件 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>
Run Code Online (Sandbox Code Playgroud)

背后的主窗口代码:

    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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

ViewModel2(没有有时属性):

    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
}
Run Code Online (Sandbox Code Playgroud)

Pet*_*iho 5

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

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

<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>
Run Code Online (Sandbox Code Playgroud)

即只设置Content你的UserControl对象视图模型对象本身,从而使相应的模板用于显示在控制中的数据。没有属性的视图模型对象的模板不引用该属性,因此不会生成警告。

另一个选项,就像上面一样,也解决了您对显示警告的担忧,是创建一个“垫片”(又名“适配器”)对象,它在未知的视图模型类型和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); }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

它还实现了该HasSometimes属性,以便视图可以相应地动态调整自身。最后,它还代理底层PropertyChanged事件,以配合委托的属性本身。

要使用它,需要一些代码隐藏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)));
    }
}
Run Code Online (Sandbox Code Playgroud)

有了这个,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>
Run Code Online (Sandbox Code Playgroud)

请注意,顶级Grid元素的's属性DataContext设置为UserControl'sViewModelWrapper属性,以便包含的元素使用该对象而不是父代码分配的视图模型。

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

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

  • 它可用于在与UserControl声明视图模型类型的程序集不同的程序集中声明对象的情况,以及后者不能被前者引用的情况。
  • 它消除了基于模板的方法所需的冗余。即,不必复制/粘贴模板的共享元素,此方法对整个视图使用单个 XAML 结构,并根据需要显示或隐藏该视图的元素。

为了完整起见,这里是上面的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));
    }
}
Run Code Online (Sandbox Code Playgroud)

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