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

时间:2018-05-03 11:12:14

标签: c# wpf multibinding

我有一个列表视图Item 我想要折叠列表行中的一些项目 我正在使用网格来编辑数据模板。 我尝试使用多重绑定参数 但是我得到了#34; DependencyProperty.UnsetValue" 会为代码示例感到高兴。

如果我使用1个参数都很好。

有人可以向我解释如何使用多重绑定 会对代码示例感到满意。 感谢。

1 parmae​​ter的工作代码我可以使用此代码:

<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();
    }
 }

1 个答案:

答案 0 :(得分:0)

您遗漏的最重要的部分是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();
        }

    }
}

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