c# - WPF 扩展器事件行为
问题描述
我有一个带有 GroupStyle 的数据网格,里面有一个扩展器。由于我的应用程序的性质,我需要动态创建 Expander 的内容,因此我放置了一个事件 Onloaded 来创建列。这是代码:
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander x:Name="exp" Loaded="OnLoaded" Background="#dedede" HorizontalAlignment="Left" HorizontalContentAlignment="Left" PreviewMouseLeftButtonUp="Expander_MouseDown" BorderThickness="0 0 0 1" BorderBrush="#d0d0d0" Padding="2,0,0,0" Style="{StaticResource StatusGroupExpanderStat}">
<Expander.Content>
<ItemsPresenter />
</Expander.Content>
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
后面的代码:
private void OnLoaded(object sender, RoutedEventArgs e)
{
StackPanel stackPanel = new StackPanel();
stackPanel.Orientation = Orientation.Horizontal;
stackPanel.Height = 30;
Expander exp = sender as Expander;
string currDate = ((dynamic)exp.DataContext).Name;
// Espando il primo Expander
Statistic statRec = BaseData.FirstOrDefault();
if (statRec != null)
exp.IsExpanded = statRec.Data == currDate && exp.IsExpanded;
// Item da usare per valorizzare le colonne
Statistic statItem = BaseDataParents.FirstOrDefault(d => d.Data == currDate);
// Ciclo per creare le colonne
foreach (var column in GridData.Columns.Where(c => !String.IsNullOrEmpty(c.Header.ToString())))
{
DataGridColumn col = GridData.Columns.FirstOrDefault(c => c.Header != null && c.Header.ToString() == column.Header.ToString());
if (col == null) continue;
TextBlock textBlock = new TextBlock
{
Width = col.ActualWidth,
Padding = new Thickness(13, 8, 0, 2),
Text = FormatValue(statItem, GetColumnNameByKey(column.Header.ToString())),
Style = this.FindResource("HeaderTextBlock") as Style,
ToolTip = FormatValue(statItem, GetColumnNameByKey(column.Header.ToString()))
};
stackPanel.Children.Add(textBlock);
}
exp.Header = stackPanel;
}
基本上,我注意到在 evetytime 我折叠了一个扩展器,触发了 OnLoaded 事件并重新创建了我的列;我不想要这种行为,有没有机会做到这一点?
解决方案
您可以处理该Expanded
事件并可能使用一个bool
字段来确定是否Expander
已经初始化过一次:
private bool _isInitialized;
private void exp_Expanded(object sender, RoutedEventArgs e)
{
if (!_isInitialized)
{
StackPanel stackPanel = new StackPanel();
stackPanel.Orientation = Orientation.Horizontal;
stackPanel.Height = 30;
...
_isInitialized = true;
}
}
推荐阅读
- angular - 如何将不是使用 Angular CLI 创建的 Angular 应用程序迁移到 Angular CLI 项目?
- javascript - 确定是否安装了应用程序,如果已安装,则在应用程序中打开链接,否则导航到应用程序商店
- rsa - 如何使用从数据库pem格式phpseclib获得的私钥
- c++ - C++ STL-插入器
- scala - 为什么在 Scala 中不推荐 return ?
- mysql - 如何检查两个表或子查询结果中存在的属性
- java - 来自 Java 列表的 Protobuff 消息
- >
- backup - 是否可以使用 haproxy 作为 haproxy 的备份?
- sapui5 - 绑定没有模型名称的片段
- jquery - 如何使用 jquery 隐藏用于签名页面的导航栏,以便共享布局仍显示在其他文档上?