WPF 数据网格行 header 切换按钮视觉状态从代码隐藏更改
WPF datagrid row header toggle button visualstate change from code behind
在 WPF 中,我在数据网格行 header 中使用切换按钮来显示每个 parent 数据网格行作为 child 数据网格的详细可见性。当我单击切换按钮时,相应 parent 数据网格行的视觉状态将更改为 "Checked" 状态。它工作正常。但与此同时,我想将所有其他 parent 数据网格行的视觉状态设为 "Unchecked" 或 "Normal",而无需单击每个切换按钮。我正在使用以下代码。但它不能正常工作。但是如果我点击每个切换按钮,那么它的视觉状态就会正确改变。
private void ToggleButton_Click(object sender, RoutedEventArgs e)
{
DependencyObject obj = (DependencyObject)e.OriginalSource;
while (!(obj is DataGridRow) && (obj != null))
{
obj = VisualTreeHelper.GetParent(obj);
}
if (obj is DataGridRow)
{
if ((obj as DataGridRow).DetailsVisibility == Visibility.Visible)
{
(obj as DataGridRow).IsSelected = false;
(obj as DataGridRow).DetailsVisibility = System.Windows.Visibility.Collapsed;
}
else if ((obj as DataGridRow).DetailsVisibility == Visibility.Collapsed)
{
for (int i = 0; i < Data_Grid.Items.Count; i++)
{
DataGridRow itm = GetDataGridRowitem(i);
itm.IsSelected = false;
itm.DetailsVisibility = System.Windows.Visibility.Collapsed;
VisualStateManager.GoToElementState(itm, "Unchecked", true);
}
DataTemplate dt = FindResource("tocchild") as DataTemplate;
Data_Grid.RowDetailsTemplate = dt;
(obj as DataGridRow).IsSelected = true;
(obj as DataGridRow).DetailsVisibility = Visibility.Visible;
VisualStateManager.GoToElementState((obj as DataGridRow), "Checked", true);
}
}}
我也试过了,
VisualStateManager.GoToState((obj as DataGridRow), "Checked", true);
请帮帮我。
谢谢
将 datagrid
的第一列设置为此
<DataGridTemplateColumn CellTemplate="{StaticResource ExpandDetails}" Width="30" />
并将此dataTemplate
放入资源
<DataTemplate x:Key="ExpandDetails">
<ToggleButton Name="ShowDetails" Height="20"
IsChecked="{Binding RelativeSource={RelativeSource AncestorType=DataGridRow},
Path=DetailsVisibility,
Converter={StaticResource VisbilityToBooleanConverter},
Mode=TwoWay}"
Margin="4" HorizontalAlignment="Right" FlowDirection="LeftToRight" BorderThickness="0" />
</DataGridTemplateColumn>
然后像这样创建VisbilityToBooleanConverter
public class VisbilityToBooleanConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return (Visibility)value == Visibility.Visible;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return (bool)value ? Visibility.Visible : Visibility.Collapsed;
}
}
试试这个,
class VisibilityConverter : IValueConverter
Change Visual State
我通过将值 Toggle Button IsChecked 设置为 True 和 false 来解决它。
FrameworkElement tb = GetTemplateChildByName(row, "RowHeaderToggleButton");
(tb as ToggleButton).IsChecked = false;
GetTemplateChildByName 在哪里,
public static FrameworkElement GetTemplateChildByName(DependencyObject parent, string name)
{
int childnum = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < childnum; i++)
{
var child = VisualTreeHelper.GetChild(parent, i);
if (child is FrameworkElement &&
((FrameworkElement)child).Name == name)
{
return child as FrameworkElement;
}
else
{
var s = GetTemplateChildByName(child, name);
if (s != null)
return s;
}
}
return null;
}
感谢大家。
在 WPF 中,我在数据网格行 header 中使用切换按钮来显示每个 parent 数据网格行作为 child 数据网格的详细可见性。当我单击切换按钮时,相应 parent 数据网格行的视觉状态将更改为 "Checked" 状态。它工作正常。但与此同时,我想将所有其他 parent 数据网格行的视觉状态设为 "Unchecked" 或 "Normal",而无需单击每个切换按钮。我正在使用以下代码。但它不能正常工作。但是如果我点击每个切换按钮,那么它的视觉状态就会正确改变。
private void ToggleButton_Click(object sender, RoutedEventArgs e)
{
DependencyObject obj = (DependencyObject)e.OriginalSource;
while (!(obj is DataGridRow) && (obj != null))
{
obj = VisualTreeHelper.GetParent(obj);
}
if (obj is DataGridRow)
{
if ((obj as DataGridRow).DetailsVisibility == Visibility.Visible)
{
(obj as DataGridRow).IsSelected = false;
(obj as DataGridRow).DetailsVisibility = System.Windows.Visibility.Collapsed;
}
else if ((obj as DataGridRow).DetailsVisibility == Visibility.Collapsed)
{
for (int i = 0; i < Data_Grid.Items.Count; i++)
{
DataGridRow itm = GetDataGridRowitem(i);
itm.IsSelected = false;
itm.DetailsVisibility = System.Windows.Visibility.Collapsed;
VisualStateManager.GoToElementState(itm, "Unchecked", true);
}
DataTemplate dt = FindResource("tocchild") as DataTemplate;
Data_Grid.RowDetailsTemplate = dt;
(obj as DataGridRow).IsSelected = true;
(obj as DataGridRow).DetailsVisibility = Visibility.Visible;
VisualStateManager.GoToElementState((obj as DataGridRow), "Checked", true);
}
}}
我也试过了,
VisualStateManager.GoToState((obj as DataGridRow), "Checked", true);
请帮帮我。 谢谢
将 datagrid
的第一列设置为此
<DataGridTemplateColumn CellTemplate="{StaticResource ExpandDetails}" Width="30" />
并将此dataTemplate
放入资源
<DataTemplate x:Key="ExpandDetails">
<ToggleButton Name="ShowDetails" Height="20"
IsChecked="{Binding RelativeSource={RelativeSource AncestorType=DataGridRow},
Path=DetailsVisibility,
Converter={StaticResource VisbilityToBooleanConverter},
Mode=TwoWay}"
Margin="4" HorizontalAlignment="Right" FlowDirection="LeftToRight" BorderThickness="0" />
</DataGridTemplateColumn>
然后像这样创建VisbilityToBooleanConverter
public class VisbilityToBooleanConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return (Visibility)value == Visibility.Visible;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return (bool)value ? Visibility.Visible : Visibility.Collapsed;
}
}
试试这个,
class VisibilityConverter : IValueConverter
Change Visual State
我通过将值 Toggle Button IsChecked 设置为 True 和 false 来解决它。
FrameworkElement tb = GetTemplateChildByName(row, "RowHeaderToggleButton");
(tb as ToggleButton).IsChecked = false;
GetTemplateChildByName 在哪里,
public static FrameworkElement GetTemplateChildByName(DependencyObject parent, string name)
{
int childnum = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < childnum; i++)
{
var child = VisualTreeHelper.GetChild(parent, i);
if (child is FrameworkElement &&
((FrameworkElement)child).Name == name)
{
return child as FrameworkElement;
}
else
{
var s = GetTemplateChildByName(child, name);
if (s != null)
return s;
}
}
return null;
}
感谢大家。