Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I don't know if this is a "better" way, since this remains pretty ugly, but I personnaly did like this:</p> <ul> <li>make the template in xaml</li> <li>use a multibind that takes the current binding + a binding to the column to get the "correct" dataContext (i.e.: the cell instead of the row)</li> <li>use a converter on this binding to get the value of the property you like, an optionally add a parameter if you have many properties to retrieve. </li> </ul> <p>e.g.: (sorry, I did not adapt my code to suit your project, but you should be able to do it yourself from there)</p> <p>here is my dataTemplate:</p> <pre><code>&lt;DataTemplate x:Key="TreeCellTemplate"&gt; &lt;Grid&gt; &lt;TextBlock HorizontalAlignment="Left" VerticalAlignment="Center" Margin="5,0,0,0"&gt; &lt;TextBlock.Text&gt; &lt;MultiBinding Converter="{StaticResource RowColumnToCellConverter}" ConverterParameter="Text"&gt; &lt;Binding /&gt; &lt;Binding RelativeSource="{RelativeSource AncestorType=DataGridCell}" Path="Column" /&gt; &lt;/MultiBinding&gt; &lt;/TextBlock.Text&gt; &lt;/TextBlock&gt; &lt;/Grid&gt; &lt;/DataTemplate&gt; </code></pre> <p>and here is my converter:</p> <pre><code> public class RowColumnToCellConverter : MarkupExtension, IMultiValueConverter { public RowColumnToCellConverter() { } public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) { XwpfRow row = values[0] as XwpfRow; XwpfTreeColumn column = values[1] as XwpfTreeColumn; if (row == null || column == null) return DependencyProperty.UnsetValue; TreeCell treeCell = (TreeCell)row[column.DataGrid.Columns.IndexOf(column)]; switch ((string)parameter) { case "Text": return treeCell.Text; case "Expanded": return treeCell.Expanded; case "ShowExpandSymbol": return treeCell.ShowExpandSymbol; case "CurrentLevel": return new GridLength(treeCell.CurrentLevel * 14); default: throw new MissingMemberException("the property " + parameter.ToString() + " is not defined for the TreeCell object"); } } public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) { throw new NotSupportedException(); } public override object ProvideValue(IServiceProvider serviceProvider) { return new RowColumnToCellConverter(); } } </code></pre> <p>this saves the MVVM model, and I prefer this way of doing things because I really dislike using xaml parsers to make "dynamic" datatemplates, but it's still an ugly Hack from my point of view.</p> <p>I wish the guys at MS would give us a way to get cells instead of rows as dataContexts to be able to generate templated columns on the fly...</p> <p>hope this helps</p> <p><strong>EDIT:</strong> In your case, the converter ought to be a lot simpler actually (you can return the cell's instance directly if I'm not mistaken, and you don't need any parameter), but I left the more complex version nonetheless, just in case somebody else has a similar issue</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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