C# WPF根据条件隐藏数据网格中的行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19320528/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF hide row in datagrid based on condition
提问by Xaphann
I need to hide rows in datagrid based on parameters and values in the datagrid. I figured to do something like this;
我需要根据数据网格中的参数和值隐藏数据网格中的行。我想做这样的事情;
foreach (System.Data.DataRowView dr in myDataGrid.ItemsSource)
{
//Logic to determine if Row should be hidden
if (hideRow == "Yes")
{
//Hide row code
}
}
I just cannot figure how to actual hide the row. Please note I don't want to remove the row form the datagrid or the item source.
我只是无法弄清楚如何实际隐藏该行。请注意,我不想从数据网格或项目源中删除行。
采纳答案by progpow
If hideRow is not a field of the table (i.e. not a column in the DataGridRow):
如果 hideRow 不是表的字段(即不是 DataGridRow 中的列):
<DataGrid.RowStyle>
<Style TargetType="{x:Type DataGridRow}">
<Style.Triggers>
<DataTrigger Binding="{Binding AnyProp, Converter={StaticResource hiddenConverter}}" Value="True">
<Setter Property="Visibility" Value="Collapsed"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
And realize Converter with your logic. The type of the bound variable, AnyProp above, will be yourPropertyType below. AnyProp could be any of the columns in the row.
并用您的逻辑实现转换器。绑定变量的类型,上面的 AnyProp,下面是 yourPropertyType。AnyProp 可以是行中的任何列。
[ValueConversion(typeof(yourPropType), typeof(bool))]
public class hiddenConverter: IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (hideRow == "Yes")
{
return true;
}
else
{
return false;
}
}
}
'value' will be AnyProp, and it can be used in the logic that determines whether or not to show the row, or that decision can be based on something else entirely, such as 'hideRow' in the example.
'value' 将是 AnyProp,它可以用于确定是否显示行的逻辑中,或者该决定可以完全基于其他内容,例如示例中的 'hideRow'。
回答by Nitin
You can do this in Datagrid.ItemContainerStyle instead of doing it in codebehind...
您可以在 Datagrid.ItemContainerStyle 中执行此操作,而不是在代码隐藏中执行此操作...
<DataGrid>
<DataGrid.ItemContainerStyle>
<Style TargetType="DataGridRow">
<Style.Triggers>
<DataTrigger Binding="{Binding PROPERTY}" Value="VALUE">
<Setter Property="Visibility" Value="Collapsed"/>
回答by Peter Huber
Use a CollectionViewSource to link the DataGrid with your business data. The CollectionViewSource fires a filter event for every row. In this event, your code can decide if the row should be displayed.
使用 CollectionViewSource 将 DataGrid 与您的业务数据链接起来。CollectionViewSource 为每一行触发一个过滤器事件。在这种情况下,您的代码可以决定是否应显示该行。
Add to your XAML:
添加到您的 XAML:
<Window.Resources>
<CollectionViewSource x:Key="sampleViewSource" CollectionViewType="ListCollectionView"/>
</Window.Resources>
<DataGrid DataContext="{StaticResource sampleViewSource}" ItemsSource="{Binding}"
AutoGenerateColumns="False">
Add the following to your code behind file:
将以下内容添加到您的代码隐藏文件中:
stocksViewSource = ((System.Windows.Data.CollectionViewSource)(FindResource("sampleViewSource")));
sampleViewSource.Filter += sampleViewSource_Filter;
Create the filter eventhandler. You can get the row data from e.Item. By setting e.Accepted you can control if the row should be displayed.
创建过滤器事件处理程序。您可以从 e.Item 中获取行数据。通过设置 e.Accepted,您可以控制是否应显示该行。
回答by Priyanka
<Window x:Class="ProjectName.ClassName"
xmlns:C="clr-namespace:ProjectName.FolderName"> //Folder containing 'VisibilitySetter' class.
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
<Window.Resources>
<C:VisibilitySetter x:Key="VisibilitySetter" />
</Window.Resources>
<DataGrid ItemsSource="{Binding SomeObservableCollectionProperty}">
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="Visibility" Value="{Binding Path=., Converter={StaticResource VisibilitySetter}, ConverterParameter=1}" />
</Style>
</DataGrid.RowStyle>
</DataGridTextColumn>
<DataGridTextColumn Binding="{Binding PropertyName1}" />
<DataGridTextColumn Binding="{Binding PropertyName2}" />
//................
</DataGrid>
</Window >
VisibilitySetter is a Class that Implements IValueConverter. Here is the Class...
VisibilitySetter 是一个实现 IValueConverter 的类。这里是课堂...
public class VisibilitySetter:IValueConverter
{
public object Convert(object value,Type targetType,object parameter,CultureInfo culture)
{
if(parameter.ToString() == "1") //Parameter is set in the xaml file.
{
return SetVisibilityBasedOn(value);
}
return null;
}
private object SetVisibilityBasedOn(object value)
{
if(value is SomeObject obj && obj.value == "SomeValue") //Checks the value of the object
{
return Visibility.Collapsed; //Hides the row. It Returns visibility based on the value of the row.
}
return null;
}
}