扩展按钮在我的wpf数据网格中无法正常工作。我使用以下模板进行扩展器按钮。
<!-- MouseOver, Pressed behaviours-->
<Trigger Property="IsMouseOver"
Value="true">
<Setter Property="Stroke"
Value="#FF3C7FB1"
TargetName="Circle"/>
<Setter Property="Stroke"
Value="#222"
TargetName="Sign"/>
</Trigger>
<Trigger Property="IsPressed"
Value="true">
<Setter Property="Stroke"
Value="#FF526C7B"
TargetName="Circle"/>
<Setter Property="StrokeThickness"
Value="1.5"
TargetName="Circle"/>
<Setter Property="Stroke"
Value="#FF003366"
TargetName="Sign"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
<!-- Simple Expander Template-->
<ControlTemplate x:Key="SimpleExpanderTemp" TargetType="{x:Type Expander}">
<DockPanel>
<ToggleButton x:Name="ExpanderButton"
DockPanel.Dock="Top"
Template="{StaticResource SimpleExpanderButtonTemp}"
Content="{TemplateBinding Header}"
IsChecked="{Binding Path=IsExpanded, RelativeSource={RelativeSource TemplatedParent}}"
OverridesDefaultStyle="True"
Padding="1.5,0">
</ToggleButton>
<ContentPresenter x:Name="ExpanderContent"
Grid.Row="1"
Visibility="Collapsed"
DockPanel.Dock="Bottom"/>
</DockPanel>
<ControlTemplate.Triggers>
<Trigger Property="IsExpanded" Value="True">
<Setter TargetName="ExpanderContent" Property="Visibility" Value="Visible"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
我还要为datagridtemplatecolumn添加以下代码。
<DataGridTemplateColumn Width="27">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Expander Template="{StaticResource SimpleExpanderTemp}" Expanded="Expander_Expanded" Collapsed="Expander_Collapsed"/>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
但是如果我想在我的wpf数据网格中扩展一个扩展器按钮,其他一些扩展器按钮也在扩展(不是每次都有),而且有些扩展器按钮正在折叠。
此xaml代码中的错误是什么?
扩展器事件是,
private void Expander_Expanded(object sender, RoutedEventArgs e)
{
ContentControl cc = sender as ContentControl;
Expander exp = cc as Expander;
var itemsSource = objDatagrid.ItemsSource as IEnumerable;
if (itemsSource != null)
{
foreach (var item in itemsSource)
{
var row = objDatagrid.ItemContainerGenerator.ContainerFromItem(item) as DataGridRow;
if (row != null)
{
row.IsSelected = false;
}
}
}
for (var vis = sender as Visual; vis != null; vis = VisualTreeHelper.GetParent(vis) as Visual)
if (vis is DataGridRow)
{
var row = (DataGridRow)vis;
row.IsSelected = true;
if (exp.IsExpanded)
{
row.DetailsVisibility = Visibility.Visible;
exp.ExpandDirection = ExpandDirection.Down;
}
break;
}
}
private void Expander_Collapsed(object sender, RoutedEventArgs e)
{
var itemsSource = objDatagrid.ItemsSource as IEnumerable;
if (itemsSource != null)
{
foreach (var item in itemsSource)
{
var row = objDatagrid.ItemContainerGenerator.ContainerFromItem(item) as DataGridRow;
if (row != null)
{
row.IsSelected = false;
}
}
}
for (var vis = sender as Visual; vis != null; vis = VisualTreeHelper.GetParent(vis) as Visual)
if (vis is DataGridRow)
{
var row = (DataGridRow)vis;
row.IsSelected = true;
row.DetailsVisibility = row.DetailsVisibility == Visibility.Visible ? Visibility.Collapsed : Visibility.Visible;
break;
}
}
答案 0 :(得分:0)
您可以为dataGrid尝试RowDetailsTemplate。同时设置RowDetailsVisibilityMode =“VisibleWhenSelected”
<DataGrid SelectionMode="Extended" ItemsSource="{Binding}" RowDetailsVisibilityMode="VisibleWhenSelected">
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="5">
<!-- Content -->
</StackPanel>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
<DataGrid.Columns>
<!-- Columns -->
</DataGrid.Columns>
</DataGrid>
</Grid>