2014-03-14 60 views
0

我正在使用帶有多個固定列的DataGrid。 此外,我正在動態添加包含CheckBox的標題的自定義樣式的列。在自定義樣式中進行綁定

這裏的風格:

  <Style TargetType="DataGridColumnHeader" x:Key="CustomerColumnHeader"> 
       <Setter Property="ContentTemplate"> 
        <Setter.Value> 
         <DataTemplate> 
          <Grid HorizontalAlignment="Center"> 
           <Grid.RowDefinitions> 
            <RowDefinition Height="*"/> 
            <RowDefinition Height="*"/> 
           </Grid.RowDefinitions> 
           <TextBlock Grid.Row="0" HorizontalAlignment="Center" Margin="3,0,3,0" Text="{Binding}"/> 
           <StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Center"> 
            <CheckBox Margin="0,2,5,0"/> 
            <TextBlock HorizontalAlignment="Stretch" TextAlignment="Center" Text="Interpolate"/> 
           </StackPanel> 
          </Grid> 
         </DataTemplate> 
        </Setter.Value> 
       </Setter> 
       <Setter Property="BorderBrush" Value="Black"/> 
       <Setter Property="BorderThickness" Value="0,1,1,1"/> 
      </Style> 

這是我如何添加列,並設置自己的風格:

   grid.Columns.Add(new CustomDataGridTextColumn() 
       { 
        HeaderStyle = (Style)grid.Resources["CustomerColumnHeader"], 
        Header = "Test", 
        IsReadOnly = false, 
        Binding = new Binding(binding), 
        Interpolate = true 
       }); 

編輯:這裏是我的專欄類的樣子:

class CustomDataGridTextColumn : DataGridTextColumn 
{ 
    public bool Interpolate { get; set; } 
} 

這只不過是一個DataGridTextColumn與添加屬性。

我怎樣才能綁定風格的CheckBoxCustomDataGridTextColumnInterpolate屬性的IsChecked財產?

回答

1

您可以使用RelativeSource Binding嘗試將Checkbox.IsChecked屬性綁定到您的自定義列屬性。試試這個:

<CheckBox Margin="0,2,5,0" IsChecked="{Binding Interpolate, RelativeSource={ 
    RelativeSource AncestorType={x:Type YourXamlPrefix:CustomDataGridTextColumn}}}" /> 

顯然不過,如果這個工程的話,那麼這將工作時Style被應用於具有CustomDataGridTextColumn類型的父對象,你已經正確宣佈YourXamlPrefix命名空間XAML中自定義列的CLR名稱空間。

+0

感謝您的建議。現在我幾乎完全放棄了原來的方法,因爲它已經耗費了太多時間。 :) –