网格模板的WPF多重绑定可见性

时间:2018-07-09 10:57:00

标签: c# wpf xaml

我有一个列表视图,每个列表视图都包含几列数据。该列表视图包含2个网格,其中1个网格是标题。 和一个显示所有相关数据的secound网格。 我要执行的操作是隐藏某些数据,以限制数据中某些字段的值。

我该怎么做?我收到错误时尝试了multiyBinding:

“不能在'Binding'类型的'Path'属性上设置'Binding'。只能在DependencyObject的DependencyProperty上设置'Binding'。”

很高兴有一个代码示例

这是我编写的代码

<ListView x:Name="LVGuiCoreBus" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Auto" MouseDown="LVGui_MouseDown"  Grid.Row="1"  VirtualizingPanel.IsContainerVirtualizable="True" VirtualizingPanel.IsVirtualizing="True"  VirtualizingPanel.IsVirtualizingWhenGrouping="True"  SelectionChanged="LVGuiCoreBus_SelectionChanged">                                     
                <ListView.ItemTemplate>
                        <DataTemplate>                      
                            <VirtualizingStackPanel >
                                <Grid VirtualizingPanel.VirtualizationMode="Recycling">                           
                                    <Grid.Visibility>
                                        <MultiBinding>
                                            <Binding Path="{Binding Source, Converter={StaticResource VisiblieGroupFilterBySourcecs},UpdateSourceTrigger=PropertyChanged}"></Binding>
                                            <Binding Path="{Binding Destination, Converter={StaticResource VisiblieGroupFilterBySourcecs},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},UpdateSourceTrigger=PropertyChanged}" Grid.Column="2" Background="{ Binding Source, Converter={StaticResource BackGroundFilterConverterBySource}}" />
                                    <TextBlock Text="{Binding Destination}" Foreground="{Binding Destination,Converter={StaticResource CoreBusPanelModuleColorConverter},UpdateSourceTrigger=PropertyChanged}" Grid.Column="4" Background="{ Binding Destination, Converter={StaticResource BackGroundFilterConvertorByDestantation}}"  />
                                    <TextBlock Text="{Binding Module}" Grid.Column="6" HorizontalAlignment="Center" Background="{ Binding Module, Converter={StaticResource BackGroundFilterByModule},UpdateSourceTrigger=PropertyChanged}"   />
                                    <TextBlock Text="{Binding Controll}" Grid.Column="8"  Background="{ Binding Controll, Converter={StaticResource BackGRoundFilterByControll},UpdateSourceTrigger=PropertyChanged}" />
                                    <TextBlock Text="{Binding Command}" Grid.Column="10" HorizontalAlignment="Center"  Background="{ Binding Command, Converter={StaticResource BackGroundFilterByCommand},UpdateSourceTrigger=PropertyChanged}" />
                                    <TextBlock Text="{Binding HSCommand}" Grid.Column="12" HorizontalAlignment="Center"  Background="{ Binding HSCommand, Converter={StaticResource BackGroundFilterByHsCommand},UpdateSourceTrigger=PropertyChanged}" />
                                    <TextBlock Text="{Binding Data_Str}"  Grid.Column="14" />                                                                
                        </Grid>
                            </VirtualizingStackPanel>
                        </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>

1 个答案:

答案 0 :(得分:1)

您应该按照以下两个步骤更改代码:

  1. 将您的Binding标记扩展名更改为Binding对象。
  2. 在您的`MultiBinding中添加一个IMultiValueConverter

因此,您可以像这样更改MultiBinding的用法:

<MultiBinding Converter="{StaticResource YouShouldWriteANewMultiBindingConverterHere}">
    <Binding Path="Source" Converter="{StaticResource VisiblieGroupFilterBySourcecs}" UpdateSourceTrigger="PropertyChanged" />
    <Binding Path="Destination" Converter="{StaticResource VisiblieGroupFilterBySourcecs}" UpdateSourceTrigger="PropertyChanged" />
</MultiBinding>

首先,将{Binding xxx, Converter=xxx}用作MarkupExtension,只能将其用于依赖项属性。但是,当您在Binding中使用MultiBinding时,会将其视为Binding对象而不是MarkupExtension。因此您应该像设置普通XAML对象一样设置Binding属性。

第二,MultiBinding需要一个IMultiValueConverterStringFormat属性集。

public class YouShouldWriteANewMultiBindingConverterHere : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        => throw new NotImplementedException();

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        => throw new NotImplementedException();
}

通过阅读以下内容,您可以了解有关MarkupExtensionMultiBindingIMultiValueConverter的更多信息: