LoginSignup
1
1

More than 5 years have passed since last update.

DataGridTemplateColumn内のコントロールにコードビハインドからアクセスする-其の1-

Last updated at Posted at 2016-08-25

やりたいこと

コードビハインドからDataGridTemplateColumn内のコントロールにアクセスする。
動的にバインドしたかった。

方法

xaml
<DataGrid Name="MainGrid">
  <DataGrid.Columns>
    <DataGridTemplateColumn IsReadOnly="False">
      <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
          <CheckBox Name="CheckBox"/> ★これにアクセスしたい
        </DataTemplate>
      </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
  </DataGrid.Columns>
</DataGrid>
C#
var templateColumn = ZeroDataGrid.Columns[0] as DataGridTemplateColumn;
var checkBox = templateColumn.CellTemplate.LoadContent() as CheckBox;
checkBox.SetBinding(CheckBox.IsCheckedProperty,
new Binding()
{
    Path = new PropertyPath("プロパティ名"),
    Mode = BindingMode.TwoWay,
});

駄目っぽい?

どうも上の方法だと新しいインスタンスが返却されているみたい?
其の2に続く

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1