将命令绑定到一个复选框,用于在wpf中的xamdatagrid内部onclick无法正常工作

时间:2017-07-28 06:26:55

标签: c# wpf checkbox mvvm commandbinding

我的XamDataGrid中有一个复选框,如下所示: -

<igDp:UnboundField Width="1*" Label="{LocText props:Resources.GROUPLIST_SYNC}" BindingMode="TwoWay"  BindingPath="IsSynchronise.Value" Converter="{StaticResource BoolToUMDirectoryFilter}" ConverterParameter="Enabled"  ToolTip="{LocText props:Resources.GROUPLIST_SYNC}">
    <igDp:UnboundField.Settings>
        <igDp:FieldSettings AllowEdit="True">
            <igDp:FieldSettings.LabelPresenterStyle >
                <Style  TargetType="igDp:LabelPresenter"  BasedOn="{StaticResource GmsLabelStyle }">
                    <Setter Property="AutomationProperties.AutomationId" Value="Group_SYNC"></Setter>
                </Style>
            </igDp:FieldSettings.LabelPresenterStyle>
            <igDp:FieldSettings.CellValuePresenterStyle>
                <Style TargetType="{x:Type igDp:CellValuePresenter}">
                    <Setter Property="Margin" Value="2"></Setter>
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="igDp:CellValuePresenter">
                                <CheckBox Name="chkSynchronise"  IsChecked="{Binding Path=DataContext.DataItem.IsSynchronise.Value,  RelativeSource={ RelativeSource Mode=TemplatedParent}}"
                                              HorizontalAlignment="Center"  Command="{Binding SynchroniseGroups,RelativeSource={RelativeSource Mode=Self}}" HorizontalContentAlignment="Left" >

                                </CheckBox>
                                <!--<CheckBox IsChecked="{Binding Path=DataContext.DataItem.IsSynchronise.Value, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged, RelativeSource={ RelativeSource Mode=TemplatedParent}}"  
                                                           Command="{Binding SynchroniseGroups,RelativeSource={RelativeSource Mode=Self}}"
                                                          HorizontalAlignment="Center" HorizontalContentAlignment="Left"  >

                                </CheckBox>-->
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </igDp:FieldSettings.CellValuePresenterStyle>
        </igDp:FieldSettings>
    </igDp:UnboundField.Settings>
</igDp:UnboundField>

那么,我应该如何将一个命令绑定到checkbox以便使用click来处理它并获取在我的ViewModel中检查和取消选中的行为?任何帮助将非常感激。提前谢谢。

1 个答案:

答案 0 :(得分:0)

首先我要添加一个旁注:自infragistics 14.2版以来,未绑定字段已过时(look here)

为了将布尔值绑定到XamDataGrid,我更喜欢使用CheckBoxField

班级:

public class YourClass : NotificationObject
{
    private bool _checkBoxValue;

    public bool CheckboxValue
    {
        get
        {
            return this._checkBoxValue;
        }

        set
        {
            if (this._checkBoxValue != value)
            {
                this._checkBoxValue = value;

                // Do something: Event, Method, ...

                this.RaisePropertyChanged(nameof(this.CheckboxValue));
            }
        }
    }
}

XAML:

 <igDp:FieldLayout>
     <igDp:CheckBoxField BindingType="UseAlternateBinding"
                         Name="CheckerField"
                         Label="YourCheckerFieldLabel"
                         ToolTip="YourTooltip"
                         AlternateBinding="{Binding CheckboxValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                         />
 </igDp:FieldLayout>