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

时间:2017-10-13 19:37:11

标签: c# wpf xaml mvvm

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

我知道以下问题/答案: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>

SubControl 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>

MainWindow Code Behind:

    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(没有Some属性):

    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
}

1 个答案:

答案 0 :(得分:2)

有许多不同的方法可以接近您的场景。对于它的价值,你已经拥有的解决方案对我来说似乎是合理的。你得到的警告(我认为你在谈论输出到调试控制台的错误信息)是相当无害的。它确实意味着潜在的性能问题,因为它表明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>

即。只需将Content对象的UserControl设置为视图模型对象本身,以便使用适当的模板在控件中显示数据。视图模型对象的模板没有属性,不引用该属性,因此不会生成警告。

另外一个选项,就像上面的内容也解决了你对所显示的警告的关注,就是创建一个&#34; shim&#34; (a.k.a。&#34; adapter&#34;)在未知视图模型类型和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); }
    }
}

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

它还实现了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)));
    }
}

有了这个,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设置为UserControl的{​​{1}}属性,以便包含的元素使用该对象而不是父代码分配的视图模型。

(您可以忽略ViewModelWrapper XML命名空间......那只是因为Stack Overflow的XAML格式被使用默认XML命名空间的p:元素弄糊涂了。)< / p>

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

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

为了完整性,以下是UserControl类使用的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)); } } 接口。代码更简单,更易于编写,更易于阅读,并且不易出错。