Note that there are some explanatory texts on larger screens.

plurals
  1. POBinding in a WPF data grid text column
    text
    copied!<p>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 template:</p> <pre><code>&lt;TextBlock Text="{Binding Path=Name}" FontFamily="{Binding Path=Name}"/&gt; </code></pre> <p>This worked just fine. So, I tweaked the data structure (Name became Font.Name) and moved onto a data grid to try this:</p> <pre><code>&lt;dg:DataGridTextColumn Binding="{Binding Font.Name}" FontFamily="{Binding Font.Name}" IsReadOnly="True" Header="Font"/&gt; </code></pre> <p>Now the font names are all displayed in the default font, and I get this error:</p> <pre><code>System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=Font.Name; DataItem=null; target element is 'DataGridTextColumn' (HashCode=56915998); target property is 'FontFamily' (type 'FontFamily') </code></pre> <p>A few Google results dealing with custom controls suggest changing the property from DependencyObject to FrameworkElement, but I'd have to inherit DataGridTextColumn and define my own property to do so - there must be a better way.</p> <p>I've tried several different approaches to the binding, including attempting to change just the font size with a distinct property in my data class (i.e., <code>FontSize="{Binding FontSize}"</code>). They've all resulted in the same error as above.</p> <p>Anyone know what I'm doing wrong here?</p> <p><strong>Edit:</strong></p> <p>Thanks to Jared's reply, I found the following:</p> <p><a href="http://blogs.msdn.com/jaimer/archive/2008/11/22/forwarding-the-datagrid-s-datacontext-to-its-columns.aspx" rel="noreferrer">http://blogs.msdn.com/jaimer/archive/2008/11/22/forwarding-the-datagrid-s-datacontext-to-its-columns.aspx</a></p> <p>The method looks sound, but I need to make a binding that references the correct element in the DataContext for each row, as opposed to sharing a single value for the entire column.</p> <p>Code behind:</p> <pre><code>fontDataGrid.DataContext = from font in new InstalledFontCollection().Families; </code></pre> <p>XAML:</p> <pre><code>Binding="{Binding Font.Name}" FontFamily="{Binding (FrameworkElement.DataContext).Font.Name, RelativeSource={x:Static RelativeSource.Self}}" </code></pre> <p>Using the above XAML clearly isn't correct, because DataContext is the entire collection of fonts. But I can't index the collection, since I don't know what the row number is (or do I?). Is there some approach I can use to achieve this?</p> <p>And a secondary question - why does the Binding attribute seem to work just fine, even without the DataContext? Is it looking at ItemsSource instead?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload