Binding in a WPF data grid text column

前端 未结 2 1882
萌比男神i
萌比男神i 2020-12-01 10:29

I\'m trying to build a data grid where one of the columns is a font name displayed in that font. Previously, I was working with a list box where I had defined the following

相关标签:
2条回答
  • 2020-12-01 11:18

    Try

    TextBlock.FontFamily="{Binding Font.Name}"
    

    Sometimes the binding system has a problem finding where a property is declared so you need to give it some help.

    0 讨论(0)
  • 2020-12-01 11:20

    Jared's answer is correct, but I've found a concrete solution that's solved my problem.

    http://blogs.msdn.com/vinsibal/archive/2008/12/17/wpf-datagrid-dynamically-updating-datagridcomboboxcolumn.aspx

    Following this example, I changed my DataGridTextColumn definition to:

    <dg:DataGridTextColumn Binding="{Binding Font.Name}" IsReadOnly="True" Header="Font">
        <dg:DataGridTextColumn.ElementStyle>
            <Style TargetType="TextBlock">
                <Setter Property="FontFamily" Value="{Binding Font.Name}" />
            </Style>
        </dg:DataGridTextColumn.ElementStyle>
    </dg:DataGridTextColumn>
    

    And I don't need to worry about the column inheriting the DataContext. This gives me the result I want.

    0 讨论(0)
提交回复
热议问题