开发者

Bind a DependencyProperty in DataGrid.ElementStyle for each Column

开发者 https://www.devze.com 2023-03-09 14:18 出处:网络
My problem seems quite simple but i can\'t solve it, i\'ve spent one day on it... I have a DataGrid and I want to be able to enabled/disable the TextWrapping on each column. The Wrapping is enable us

My problem seems quite simple but i can't solve it, i've spent one day on it...

I have a DataGrid and I want to be able to enabled/disable the TextWrapping on each column. The Wrapping is enable using a DependencyProperty from the controller.

It seems my binding path isn't good due to "datacontext".

What will be the good binding path ?

Here is an extract of the code :

The DependencyProperty in the controller :

public class ControlerDataConsult : DependencyObject
{
     public static readonly DependencyProperty SelectDataList_Column0IsWrappingProperty = DependencyProperty.Register("SelectDataList_Column0IsWrapping", typeof(TextWrapping), typeof(ControlerDataConsult), new UIPropertyMetadata(TextWrapping.NoWrap));
}

The "ControlerDataConsult" is used as DataContext for the XAML Page.

The Xaml in the DataGrid.

<dg:DataGrid CanUserSortColumns="False" IsReadOnly="True" Name="SelectedList" SelectionMode="Extended"   SelectionChanged="SelectedList_SelectionChanged"
      HeadersVisibility="Column"  Margin="5,14,5,-4" GridLinesVisibility="all" AutoGenerateColumns="False" ItemsSource="{Binding Path=SelectDataList}"
      BorderThickness="1" BorderBrush="{DynamicResource clBLACK}" Loaded="SelectedList_Loaded" >
      <dg:DataGrid.Columns>
           <dg:DataGridTextColumn Width="50"  Header="" HeaderStyle="{StaticResource DataGridHeaderStyle}" Binding="{Binding Index}"/>
               <dg:DataGridTextColumn Visibility="{Binding (FrameworkElement.DataContext).SelectDataList_Column0IsVisible,  RelativeSource={x:Static RelativeSource.Self},Converter={StaticResource VisibilityConverter}}"
                    Header="{Binding (FrameworkElement.DataContext).SelectDataList_Column0Title, RelativeSource={x:Static RelativeSource.Self}}" 
                    HeaderStyle="{StaticResource DataGridHeaderStyle}" Binding="{Binding Col0}">
                    <dg:DataGridTextColumn.ElementStyle>
                          <Style TargetType="TextBlock">
                               <Setter Property="TextWrapping" Value="{Binding (FrameworkElement.DataContext).SelectDataList_Column0IsWrapping, RelativeSource={x:Static RelativeSource.Self}}" />
                          </Style>
                    </dg:DataGridTextColumn.ElementStyle>
               </dg:DataGridTextColumn>
       </dg:DataGrid.Columns>
</dg:DataGrid>

If i try this : <Setter开发者_StackOverflow社区 Property="TextWrapping" Value="True" /> it works as expected.

I try several bindings and can get the good one.

At runtime i've got this error :

BindingExpression path error: 'SelectDataList_Column0IsWrapping' property not found on 'object' ''DataRowView' (HashCode=4892154)'. BindingExpression:Path=(FrameworkElement.DataContext).SelectDataList_Column0IsWrapping; DataItem='TextBlock' (Name=''); target element is 'TextBlock' (Name=''); target property is 'TextWrapping' (type 'TextWrapping')

If you have any guess you'll save me :)

Thanks,


The "ControlerDataConsult" is used as DataContext for the XAML Page.

If that is the case you could try a relative source binding like this:

{Binding RelativeSource={RelativeSource AncestorType=Page}, Path=DataContext.SelectDataList_Column0IsWrapping}
0

精彩评论

暂无评论...
验证码 换一张
取 消