WPF 中网格可见性的多重绑定

MultiBinding of grid visiblity in WPF

我有一个列表视图项 我想折叠列表行中的一些项目 我正在使用网格来编辑数据模板。 我尝试使用参数的多重绑定 但是我明白了 "DependencyProperty.UnsetValue" 很高兴有代码示例。

如果我使用 1 个参数就没问题。

谁能给我解释一下如何使用多重绑定 会对代码示例感到满意。 谢谢。

1 个参数的工作代码我可以使用此代码:

<ListView x:Name="LVGuiCoreBus" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Auto" MouseDown="LVGui_MouseDown"  Grid.Row="1" >               
    <ListView.ItemTemplate>
        <DataTemplate>
            <Grid Visibility="{Binding Source, Converter={StaticResource VisiblieGroupFilterBySourcecs}}">                                                                                                                              
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="130"></ColumnDefinition>
                    <ColumnDefinition Width="10"></ColumnDefinition>
                    <ColumnDefinition Width="50"></ColumnDefinition>
                    <ColumnDefinition Width="10"></ColumnDefinition>
                    <ColumnDefinition Width="50"></ColumnDefinition>
                    <ColumnDefinition Width="10"></ColumnDefinition>
                    <ColumnDefinition Width="100"></ColumnDefinition>
                    <ColumnDefinition Width="10"></ColumnDefinition>                                                      
                    <ColumnDefinition Width="70"></ColumnDefinition>
                    <ColumnDefinition Width="10"></ColumnDefinition>
                    <ColumnDefinition Width="150"></ColumnDefinition>
                    <ColumnDefinition Width="10"></ColumnDefinition>
                    <ColumnDefinition Width="150"></ColumnDefinition>
                    <ColumnDefinition Width="10"></ColumnDefinition>
                    <ColumnDefinition Width="1*"></ColumnDefinition>
                </Grid.ColumnDefinitions>

                <TextBlock Text="{Binding DateNTimeStr}"  Foreground="Green" Grid.Column="0" />
                    <TextBlock Text="{Binding Source}" Foreground="{Binding Source,Converter={StaticResource CoreBusPanelModuleColorConverter}}" Grid.Column="2" Background="{ Binding Source, Converter={StaticResource BackGroundFilterConverterBySource}}" />
                    <TextBlock Text="{Binding Destination}" Foreground="{Binding Destination,Converter={StaticResource CoreBusPanelModuleColorConverter}}" Grid.Column="4" Background="{ Binding Destination, Converter={StaticResource BackGroundFilterConvertorByDestantation}}"  />
                    <TextBlock Text="{Binding Module}" Grid.Column="6" HorizontalAlignment="Center" Background="{ Binding Module, Converter={StaticResource BackGroundFilterByModule}}"   />
                    <TextBlock Text="{Binding Controll}" Grid.Column="8"  Background="{ Binding Controll, Converter={StaticResource BackGRoundFilterByControll}}"  />
                    <TextBlock Text="{Binding Command}" Grid.Column="10" HorizontalAlignment="Center"  Background="{ Binding Command, Converter={StaticResource BackGroundFilterByCommand}}"  />
                    <TextBlock Text="{Binding HSCommand}" Grid.Column="12" HorizontalAlignment="Center"  Background="{ Binding HSCommand, Converter={StaticResource BackGroundFilterByHsCommand}}"   />
                <TextBlock Text="{Binding Data_Str}"  Grid.Column="14"/>                                                                
            </Grid>
        </DataTemplate>
    </ListView.ItemTemplate>
    <ListView.ContextMenu>
        <ContextMenu x:Name="CMMenuCopy">
            <MenuItem x:Name="MCICopyLine" Header="Copy Line" Click="MCICopyLine_Click" ></MenuItem>
            <MenuItem x:Name="MCICopyText" Header="Copy Only Data Array" Click="MCICopyText_Click" ></MenuItem>
            <MenuItem x:Name="MCIClear" Header="Clear" Click="MCIClear_Click"></MenuItem>
        </ContextMenu>
    </ListView.ContextMenu>
</ListView>

代码多重绑定无效:

<ListView x:Name="LVGuiCoreBus" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Auto" MouseDown="LVGui_MouseDown"  Grid.Row="1" >               
    <ListView.ItemTemplate>
        <DataTemplate>
            <Grid>
                  <Grid.Visibility>
                                    <MultiBinding Converter="{StaticResource MultiValueConvertorVisibility}" UpdateSourceTrigger="PropertyChanged">
                                        <Binding ElementName="Source" Path="Visibility"  UpdateSourceTrigger="PropertyChanged"></Binding>
                                        <Binding ElementName="Destination" Path="Visibility"  UpdateSourceTrigger="PropertyChanged"></Binding>
                                        <Binding ElementName="Module" Path="Visibility"  UpdateSourceTrigger="PropertyChanged"></Binding>
                                        <Binding ElementName="Controll" Path="Visibility"  UpdateSourceTrigger="PropertyChanged"></Binding>
                                        <Binding ElementName="Command" Path="Visibility"  UpdateSourceTrigger="PropertyChanged"></Binding>
                                        <Binding ElementName="HSCommand" Path="Visibility"  UpdateSourceTrigger="PropertyChanged"></Binding>
                                    </MultiBinding>
                                </Grid.Visibility>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="130"></ColumnDefinition>
                    <ColumnDefinition Width="10"></ColumnDefinition>
                    <ColumnDefinition Width="50"></ColumnDefinition>
                    <ColumnDefinition Width="10"></ColumnDefinition>
                    <ColumnDefinition Width="50"></ColumnDefinition>
                    <ColumnDefinition Width="10"></ColumnDefinition>
                    <ColumnDefinition Width="100"></ColumnDefinition>
                    <ColumnDefinition Width="10"></ColumnDefinition>                                                      
                    <ColumnDefinition Width="70"></ColumnDefinition>
                    <ColumnDefinition Width="10"></ColumnDefinition>
                    <ColumnDefinition Width="150"></ColumnDefinition>
                    <ColumnDefinition Width="10"></ColumnDefinition>
                    <ColumnDefinition Width="150"></ColumnDefinition>
                    <ColumnDefinition Width="10"></ColumnDefinition>
                    <ColumnDefinition Width="1*"></ColumnDefinition>
                </Grid.ColumnDefinitions>

                <TextBlock Text="{Binding DateNTimeStr}"  Foreground="Green" Grid.Column="0" />
                    <TextBlock Text="{Binding Source}" Foreground="{Binding Source,Converter={StaticResource CoreBusPanelModuleColorConverter}}" Grid.Column="2" Background="{ Binding Source, Converter={StaticResource BackGroundFilterConverterBySource}}" />
                    <TextBlock Text="{Binding Destination}" Foreground="{Binding Destination,Converter={StaticResource CoreBusPanelModuleColorConverter}}" Grid.Column="4" Background="{ Binding Destination, Converter={StaticResource BackGroundFilterConvertorByDestantation}}"  />
                    <TextBlock Text="{Binding Module}" Grid.Column="6" HorizontalAlignment="Center" Background="{ Binding Module, Converter={StaticResource BackGroundFilterByModule}}"   />
                    <TextBlock Text="{Binding Controll}" Grid.Column="8"  Background="{ Binding Controll, Converter={StaticResource BackGRoundFilterByControll}}"  />
                    <TextBlock Text="{Binding Command}" Grid.Column="10" HorizontalAlignment="Center"  Background="{ Binding Command, Converter={StaticResource BackGroundFilterByCommand}}"  />
                    <TextBlock Text="{Binding HSCommand}" Grid.Column="12" HorizontalAlignment="Center"  Background="{ Binding HSCommand, Converter={StaticResource BackGroundFilterByHsCommand}}"   />
                <TextBlock Text="{Binding Data_Str}"  Grid.Column="14"/>                                                                
            </Grid>
        </DataTemplate>
    </ListView.ItemTemplate>
    <ListView.ContextMenu>
        <ContextMenu x:Name="CMMenuCopy">
            <MenuItem x:Name="MCICopyLine" Header="Copy Line" Click="MCICopyLine_Click" ></MenuItem>
            <MenuItem x:Name="MCICopyText" Header="Copy Only Data Array" Click="MCICopyText_Click" ></MenuItem>
            <MenuItem x:Name="MCIClear" Header="Clear" Click="MCIClear_Click"></MenuItem>
        </ContextMenu>
    </ListView.ContextMenu>
</ListView>
 public class MultiValueConvertorVisibility : IMultiValueConverter
 {
 public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        bool x = System.Convert.ToBoolean(values[0]);
        if (x)
        {
            return System.Windows.Visibility.Visible;
        }
        else
        {
            return System.Windows.Visibility.Collapsed;
        }
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
 }

从概念上讲,您缺少的最重要的部分是 IMultiValueConverter,因为可见性只是一个值,归根结底,您希望网格可见或不可见,因此您有告诉多重绑定如何将您的多个值组合成一个。

这里是一个如何让多重绑定起作用的例子:

网格:

<Window x:Class="WpfApp1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApp1"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">
<Window.Resources>
    <local:MultiValueConverter x:Key="MyCustomConvertor"/>
</Window.Resources>


<Grid ShowGridLines="True">
    <Grid.Visibility>
        <MultiBinding Converter="{StaticResource MyCustomConvertor}" UpdateSourceTrigger="PropertyChanged">
            <Binding Path="Visibility"/>
        </MultiBinding>
    </Grid.Visibility>
    <Grid.RowDefinitions>
        <RowDefinition Height="100"/>
        <RowDefinition Height="100"/>
        <RowDefinition Height="100"/>
    </Grid.RowDefinitions>
</Grid>

您绑定的 Viewmodel

using System.ComponentModel;
using System.Windows;

namespace WpfApp1
{
    class VM : INotifyPropertyChanged
    {
        #region WPF integration properties
        public event PropertyChangedEventHandler PropertyChanged;

        // Create the OnPropertyChanged method to raise the event
        protected void OnPropertyChanged(string name)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
        }
        #endregion

        private Visibility _visibility = Visibility.Hidden;
        public Visibility Visibility
        {
            get { return _visibility; }
            set
            {
                _visibility = value;
                OnPropertyChanged(nameof(Visibility));
            }
        }
    }
}

最后 MultiValueConverter 将多个可见性绑定转换为一个:

using System;
using System.Linq;
using System.Windows;
using System.Windows.Data;

namespace WpfApp1
{
    class MultiValueConverter : IMultiValueConverter
    {

        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {

            if (values.Contains(Visibility.Collapsed))
            {
                return Visibility.Collapsed;
            }

            if (values.Contains(Visibility.Hidden))
            {
                return Visibility.Hidden;
            }

            return Visibility.Visible;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

    }
}

您可以在转换器中放置您认为合适的任何逻辑,我只是​​在不同的可见性类型上施加了一种层次结构。