zoukankan      html  css  js  c++  java
  • Wpf DataGridCheckBoxColumn 问题

    使用DataGridCheckBoxColumn  binding一个布尔属性时,发现无法checkbox无法勾选, 并且HeaderTemplate中的checkbox无法获取到viewmodel的IsSelectAll属性,

    最后通过下列方式解决。注意放大的部分

     
    <DataGridCheckBoxColumn
                       x:Name="checkColumn"
                       Width="43"
                       Binding="{Binding Path=IsSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                       CanUserSort="False"
                       IsReadOnly="False">
                        <DataGridCheckBoxColumn.ElementStyle>
                            <Style  TargetType="CheckBox" />
                        </DataGridCheckBoxColumn.ElementStyle>
                        <DataGridCheckBoxColumn.HeaderTemplate>
                            <DataTemplate>
                                <CheckBox IsChecked="{Binding Path=DataContext.IsSelectAll, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGrid}}}" />
                            </DataTemplate>
     
                        </DataGridCheckBoxColumn.HeaderTemplate>
                    </DataGridCheckBoxColumn>

     方案二:

    通过资源绑定

     public class BindingProxy : Freezable
        {
            #region Overrides of Freezable
    
            protected override Freezable CreateInstanceCore()
            {
                return new BindingProxy();
            }
    
            #endregion
    
            public object Data
            {
                get { return (object)GetValue(DataProperty); }
                set { SetValue(DataProperty, value); }
            }
    
            // Using a DependencyProperty as the backing store for Data.  This enables animation, styling, binding, etc...
            public static readonly DependencyProperty DataProperty =
                DependencyProperty.Register("Data", typeof(object), typeof(BindingProxy), new UIPropertyMetadata(null));
        }
    添加资源
    <wpf:BindingProxy x:Key="proxy" Data="{Binding}" />

     <DataGridTemplateColumn  Visibility="{Binding Data.CanChooseMorePatients,Source={StaticResource proxy}, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource  CollapsedConverter}}" >
                            <DataGridTemplateColumn.Header>
                                <CheckBox IsChecked="{Binding Path=Data.IsAllPatientSelected,UpdateSourceTrigger=PropertyChanged, Source={StaticResource proxy} }" />
                            </DataGridTemplateColumn.Header>
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <CheckBox IsChecked="{Binding IsPatientSelected,Mode=OneWay,UpdateSourceTrigger=PropertyChanged}"  wpf:CheckboxHelper.PreMouseLeftButtonDownCommand="{Binding CheckedClickCommand}"  />
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                          
      </DataGridTemplateColumn>
  • 相关阅读:
    【nyoj-1274】信道安全(SPFA)
    【lightoj-1002】Country Roads(dijkstra变形)
    【牛客练习赛12-B】迷宫(BFS)
    【数论-逆元】复习总结
    【hdu1705】Count the grid(皮克定理)
    CSS 清除默认样式
    vue2.0项目实战使用axios发送请求
    axios基本用法
    vue2.0使用Sortable.js实现的拖拽功能
    vue2.0s中eventBus实现兄弟组件通信
  • 原文地址:https://www.cnblogs.com/karl-F/p/8073462.html
Copyright © 2011-2022 走看看