将DataContext.Property绑定到控件值

时间:2015-01-16 02:33:53

标签: c# wpf

我使DataGrid具有DataGridCheckBoxColumn。我想在DataGridCheckBoxColumn.HeaderTemplate中将DataContext的CheckedAll属性绑定到IsChecked of复选框。

这是我的数据行项目类:

public class ContractForMuster : INotifyPropertyChanged
{
    private bool _Checked;
    public bool Checked
    {
        get
        {
            return _Checked;
        }
        set
        {
            _Checked = value;
            OnPropertyChanged("Checked");
        }
    }

    public int ID { get; set; }

    public string ContractCode { get; set; }

    public string Fullname { get; set; }

    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(String propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

这是我的DataContext类:

public class ContractForMusters : INotifyPropertyChanged
{
    private bool _CheckedAll;
    public bool CheckedAll
    {
        get
        {
            return _CheckedAll;
        }
        set
        {
            _CheckedAll = value;
            OnPropertyChanged("CheckedAll");
        }
    }
    public List<ContractForMuster> models { get; set; }

    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(String propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

代码behide:

viewModel = new ContractForMusters();
entities = new List<ContractForMuster>();
entities.Add(entity); //Code add
viewModel.models = entities;
viewModel.CheckedAll = true;
this.DataContext = viewModel;

和XAML代码:

<DataGrid Name="DgdContract" Style="{StaticResource DataGridStyle}" ItemsSource="{Binding models}">
    <DataGrid.Columns>
        <DataGridCheckBoxColumn MaxWidth="50" MinWidth="30" Binding="{Binding Checked, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
            <DataGridCheckBoxColumn.HeaderTemplate>
                <DataTemplate>
                    <CheckBox x:Name="ChkAll" HorizontalAlignment="Center" Click="DgdContractCheckBoxItemChkAll_Click" IsChecked="{Binding ?}" />
                </DataTemplate>
            </DataGridCheckBoxColumn.HeaderTemplate>
            <DataGridCheckBoxColumn.ElementStyle>
                <Style>
                    <Setter Property="Control.VerticalAlignment" Value="Center" />
                    <Setter Property="Control.HorizontalAlignment" Value="Center" />
                    <Setter Property="Control.Margin" Value="5 0 0 0" />
                    <EventSetter Event="CheckBox.Click" Handler="DgdContractCheckBoxItem_Click" />
                </Style>
            </DataGridCheckBoxColumn.ElementStyle>
        </DataGridCheckBoxColumn>
        <DataGridTextColumn ElementStyle="{StaticResource IDAlignmentAlign}" Width="Auto" Binding="{Binding ID}" Header="ID" HeaderStyle="{StaticResource ColumnHeaderStyle}" />

        <DataGridTextColumn ElementStyle="{StaticResource CellAlignmentAlign}" Binding="{Binding ContractCode}" Header="Contract Code" HeaderStyle="{StaticResource ColumnHeaderStyle}" />

        <DataGridTextColumn ElementStyle="{StaticResource CellAlignmentAlign}" Binding="{Binding Fullname}" Header="Fullname" HeaderStyle="{StaticResource ColumnHeaderStyle}" />
    </DataGrid.Columns>
</DataGrid>

如何将属性CheckedAll绑定到IsChecked复选框ChkAll?

谢谢!

2 个答案:

答案 0 :(得分:0)

您可以直接绑定到根目录的datacontext。 像这样(未经测试):

(您的文件根目录)

<Window ... x:Name="root">

(你的元素)

<CheckBox x:Name="ChkAll" HorizontalAlignment="Center" 
          Click="DgdContractCheckBoxItemChkAll_Click" 
          DataContext="{Binding ElementName=root}" 
          IsChecked="{Binding CheckedAll}" />

答案 1 :(得分:0)

我已经解决了我的问题。

在Window元素中,添加x:Name =&#34; root&#34;,然后在复选框中:

<CheckBox x:Name="ChkAll" HorizontalAlignment="Center" Click="DgdContractCheckBoxItemChkAll_Click" IsChecked="{Binding DataContext.CheckedAll, ElementName=root}" />

全部谢谢!