企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
Datagrid有多个bug; 1,不支持DynamicResource的东西 2, 在Column隐藏后再显示, ColumnHeader的Tag或者DataContext为null。 解决办法: 用StaticResource ,如下面的 StaticResource DGC_ContentTemplate; 用DataGridColumnHeader的Column,来取得上层的datacontext。如, ItemsSource="{Binding Path=Column.Tag, Converter ={StaticResource DebugConverter}, Mode=OneWay, RelativeSource={RelativeSource   FindAncestor, AncestorType={x: Type DataGridColumnHeader}}}"  privatevoidInitDataGridBinding(DataGridColumnExcol,stringpath)        {           if(null==col||String.IsNullOrWhiteSpace(path) ||null==col.Header)            {               return;            }           Bindingbinding=newBinding();           binding.Mode=BindingMode.OneWay;           binding.RelativeSource=newRelativeSource(RelativeSourceMode.Self);           binding.Converter=newBoolToVisibilityConverter();           binding.Path=newPropertyPath("(FrameworkElement.DataContext).PACfgVM."+path+".CfgInfo["+PAAttachedProperty.GetName(col) +"].IsVisiable");           BindingOperations.SetBinding(col,DataGridColumnEx.VisibilityProperty,binding);           //{for combobox binding in columnHeader           col.Tag=PAAttachedProperty.GetTag(col);           DependencyPropertyDescriptordpd=DependencyPropertyDescriptor.FromProperty(DataGridColumnEx.VisibilityProperty,typeof(DataGridColumnEx));           if(dpd!=null)            {               dpd.AddValueChanged(col,delegate                {                   //                   InitDataGridColumnHeader(col,GetDataGridParent(col));                });            }           //}        }           <DataTemplatex:Key="DGC_ContentTemplate">               <TextBlockTextWrapping="Wrap"Text="{Binding}"MinWidth="20"/>           </DataTemplate>  <Stylex:Key="DataGridColumnHeaderStyle_ComboBox"TargetType="{x:TypeDataGridColumnHeader}">               <SetterProperty="ContentTemplate"Value="{StaticResourceDGC_ContentTemplate}"/>               <SetterProperty="VerticalContentAlignment"Value="Center"/>                                <SetterProperty="Template">                                                <Setter.Value>                                                                <ControlTemplateTargetType="{x:TypeDataGridColumnHeader}">                           <Grid>                                                                                                <StackPanelOrientation="Vertical"d:LayoutOverrides="Height"Margin="10">                                   <LabelForeground="Black"HorizontalContentAlignment="Left"Content="{TemplateBindingContent}"/>                                   <!--<ComboBox Margin="0,10,0,0" ItemsSource="{Binding (FrameworkElement.Column.).TimeSearchGroup, Converter={StaticResource DebugConverter}, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}"/>-->                                   <!--<ComboBox Margin="0,10,0,0"                                              ItemsSource="{Binding Path=Tag, Converter={StaticResource DebugConverter}, Mode=OneWay, RelativeSource={RelativeSource  FindAncestor, AncestorType={x:Type DataGridColumnHeader}}}"></ComboBox>-->                                   <ComboBoxMargin="0,10,0,0"                                             ItemsSource="{BindingPath=Column.Tag,Converter={StaticResourceDebugConverter},Mode=OneWay,RelativeSource={RelativeSource FindAncestor,AncestorType={x:TypeDataGridColumnHeader}}}"></ComboBox>                               </StackPanel>                                                                                                                                                                                                       <Thumbx:Name="PART_LeftHeaderGripper"HorizontalAlignment="Left"Style="{StaticResourceColumnHeaderGripperStyle}"/>                                                                                                <Thumbx:Name="PART_RightHeaderGripper"HorizontalAlignment="Right"Style="{StaticResourceColumnHeaderGripperStyle}"/>                                                                                </Grid>                                                                </ControlTemplate>                                                </Setter.Value>                                </Setter>               <EventSetterEvent="Selector.SelectionChanged"Handler="ComboBox_SelectionChanged"/>           </Style>