我使用了一个带有RowDetails面板的WPF Datagrid,其中的RowDetailsVisibilityMode设置为"VisibleWhenSelected“,SelectionMode=设置为”Extended“,这样就可以选择多个行,从而显示RowDetails,如下所示:
<dg:DataGrid x:Name="MyGrid"
ItemsSource="{Binding Path=MyItems}"
AutoGenerateColumns="True"
SelectionMode="Extended"
RowDetailsVisibilityMode="VisibleWhenSelected">
<dg:DataGrid.RowDetailsTemplate>
<DataTemplate>
<TextBlock Text="Further Details..."/>
</DataTemplate>
</dg:DataGrid.RowDetailsTemplate>
...
</dg:DataGrid>不幸的是,对于这个应用程序来说,在“选定”行上显示行详细信息并不直观,客户端希望单击多个行上的复选框来显示RowDetails窗格,但也可以在网格中滚动选择其他行。换句话说,不管DataGrid上发生了什么,都要修复显示RowDetails的行。
因此,当前滚动会关闭他们已经打开的RowDetailsPanes。我想做的是在其中一列中有一个复选框,并将RowDetails面板的可见性绑定到此属性,但我不知道如何做。问题很简单,RowDetailsPane只对datagrid中的行选择进行操作--它能以某种方式扩展到对我选择的属性进行操作吗?
先谢谢你,威尔
发布于 2009-09-24 19:15:26
查看WPF工具包的源代码,每个DataGridRow都有一个DetailsVisibility属性。
我在第一列中放了一个按钮(仅用于测试)。
<toolkit:DataGridTemplateColumn>
<toolkit:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button x:Name="buttonDetails" Content="Hello" ButtonBase.Click="Details_Click" />
</DataTemplate>
</toolkit:DataGridTemplateColumn.CellTemplate>
</toolkit:DataGridTemplateColumn>单击该按钮时,找到单击的行并切换该属性。
private void Details_Click(object sender, RoutedEventArgs e)
{
try
{
// the original source is what was clicked. For example
// a button.
DependencyObject dep = (DependencyObject)e.OriginalSource;
// iteratively traverse the visual tree upwards looking for
// the clicked row.
while ((dep != null) && !(dep is DataGridRow))
{
dep = VisualTreeHelper.GetParent(dep);
}
// if we found the clicked row
if (dep != null && dep is DataGridRow)
{
// get the row
DataGridRow row = (DataGridRow)dep;
// change the details visibility
if (row.DetailsVisibility == Visibility.Collapsed)
{
row.DetailsVisibility = Visibility.Visible;
}
else
{
row.DetailsVisibility = Visibility.Collapsed;
}
}
}
catch (System.Exception)
{
}
}我还没有探索过通过数据绑定来做到这一点。
发布于 2012-10-26 01:09:14
使用纯XAML (+ a转换器):
XAML:
<DataGrid.RowHeaderTemplate>
<DataTemplate>
<ToggleButton
IsChecked="{Binding Path=DetailsVisibility,
RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}},
Converter={StaticResource _VisibilityToNullableBooleanConverter}}"
/>
</DataTemplate>
</DataGrid.RowHeaderTemplate>转换器:
public class VisibilityToNullableBooleanConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is Visibility)
{
return (((Visibility)value) == Visibility.Visible);
}
else
{
return Binding.DoNothing;
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is bool?)
{
return (((bool?)value) == true ? Visibility.Visible : Visibility.Collapsed);
}
else if (value is bool)
{
return (((bool)value) == true ? Visibility.Visible : Visibility.Collapsed);
}
else
{
return Binding.DoNothing;
}
}
}发布于 2018-02-07 01:39:42
如果你使用(优秀的) Lambda Converters库,你可以保存额外的类。此转换器使用2个λ表达式,第一个用于Convert,第二个用于ConvertBack,例如:
public static readonly IValueConverter VisibilityToBoolean =
ValueConverter.Create<Visibility, bool>(
(e => e.Value == Visibility.Visible),
(e => e.Value ? Visibility.Visible : Visibility.Collapsed));然后,XAML如下所示(请注意,使用此方法时不需要StaticResources ):
<DataGrid.RowHeaderTemplate>
<DataTemplate>
<ToggleButton>
<ToggleButton.IsChecked>
<Binding RelativeSource="{RelativeSource AncestorType={x:Type DataGridRow}}" Path="DetailsVisibility"
Converter="{x:Static lc40:Converters.VisibilityToBoolean}"/>
</ToggleButton.IsChecked>
</ToggleButton>
</DataTemplate>
</DataGrid.RowHeaderTemplate>此处提供了Lambda转换器:
https://stackoverflow.com/questions/1471534
复制相似问题