双向绑定XamDataGrid字段

时间:2015-06-08 09:51:18

标签: wpf binding xamdatagrid

我在XamDataGrid中有以下代码:

 <igDp:Field Name="IsBlackChecked"  Label="Black Image" />

问题在于它不是双向绑定。当我单击UI中的复选框时,未设置值。

我尝试过以下解决方案:

 <igWPF:Field Name="IsBlackChecked" Label="Black Image" Width="Auto"  >
    <igWPF:Field.Settings>
      <igWPF:FieldSettings AllowEdit="True">
        <igWPF:FieldSettings.EditorStyle>
          <Style TargetType="{x:Type igWPF:XamCheckEditor}" 
            BasedOn="{StaticResource {x:Type igWPF:XamCheckEditor}}" >                                                <Setter Property="IsChecked"                                                        Value="{Binding DataItem.IsBlackChecked, Mode=TwoWay}"/>
           </Style>
         </igWPF:FieldSettings.EditorStyle>
        <igWPF:FieldSettings.CellValuePresenterStyle>
       <Style TargetType="{x:Type igWPF:CellValuePresenter}">
         <Setter Property="Template">
          <Setter.Value>
           <ControlTemplate TargetType="{x:Type igWPF:CellValuePresenter}">
            <CheckBox IsChecked="{Binding DataItem.IsBlackChecked, Mode=TwoWay}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
           </ControlTemplate>
          </Setter.Value>
         </Setter>
        </Style>
       </igWPF:FieldSettings.CellValuePresenterStyle>
      </igWPF:FieldSettings>
     </igWPF:Field.Settings>
   </igWPF:Field>

这确实为我提供了双向绑定,但它改变了单元格的样式,边框线也消失了,

如何在第一个选项中的此字段中指定双向绑定/在第二个选项中恢复边线?

1 个答案:

答案 0 :(得分:1)

<igDp:Field Name="IsBlackChecked"  Label="Black Image" />

默认情况下,上面是双向binding(您可以检查任何数据类型)。您的方案中的实际问题可能是

  1. 当您尝试检查checkbox CellvaluePresenter将消耗moues click event时。所以首先你必须选择一个单元格然后如果你点击checkbox checkbox将被检查。 (这种情况发生在我的案例中。)
  2. 您尚未在INotifyProertyChanged中实施model class。因此,当您从record标记离开(收集通知)时,更改将会反映在property(希望如此)中。
  3. 一个大缺陷:

    您已创建editorstylecellvaluepresenterstyle。只有editorstyle就足够了。(更改cellvaluepresentrer样式会改变单元格的外观和交互行为。这就是为什么border上没有cell和可能选择指示也会出错。)

    editorstyle中根据您的需要定义template/bindings复选框。 然后在功能和美学方面,解决方案都将完成。

     <Style x:Key="PrevPolLocIDStyle" TargetType="{x:Type igEditors:XamTextEditor}" BasedOn="{StaticResource {x:Type igEditors:XamTextEditor}}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch">
                        <ContentControl HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
                                        Content="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type igDP:CellValuePresenter}}, Path=Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                     Background="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type igDP:CellValuePresenter}}, Path=Background, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                   BorderThickness="0,0" >
    
                        </ContentControl>
                        <Ellipse Height="10" Width="10" VerticalAlignment="Center" HorizontalAlignment="Right" ToolTip="Single Sublocation" Margin="10,0,0,0" >
                        <Ellipse.Style>
                            <Style  TargetType="{x:Type Ellipse}">
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type igDP:DataRecordCellArea}}, Path=Record.DataItem.IsSingleInGroup}" Value="true">
                                        <Setter Property="Fill" Value="Blue"/>
                                    </DataTrigger>
                                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type igDP:DataRecordCellArea}}, Path=Record.DataItem.IsSingleInGroup}" Value="false">
                                        <Setter Property="Fill" Value="Transparent"/>
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </Ellipse.Style>
                    </Ellipse>
                    </StackPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    

    以上是controltemplate 的复杂XamTextEditor。并在fieldsetting editorstyle媒体资源中使用此款式。

     <igDP:FieldSettings EditorStyle="{StaticResource PrevPolLocIDStyle}"> 
    

    注意: 除非绝对必要,否则不要重新创建CellValuePresenter模板。您的案例只需要创建一个EditorStyle的单元格来将bool属性绑定到checkbox。简而言之,您可以在CellValuePresenter中移动EditorStyle模板代码并对绑定进行一些调整。