Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I make an AttachedProperty the target of a MultiBinding?
    text
    copied!<p>I've been working with the WPF DataGrid and trying to centralize my cell style. During this refactor, I came across a need for my cell style to know validation logic that is different for each column. I decided to provide an attached property on my column object, which would contain the result of my validation logic (with the logic being different per column) and be accessable in my DataGridCell style. Unfortunately, the MultiBinding that I've tied to the Attached Property doesn't work.</p> <p>My cell style includes a DataTrigger, where the trigger's binding path is an Attached Property. (Note that the style's TargetType is DataGridCell, which has a Column property)</p> <pre><code>&lt;DataTrigger Value="Error"&gt; &lt;DataTrigger.Binding&gt; &lt;Binding Converter="{StaticResource debugConverter}" RelativeSource="{RelativeSource Self}" Path="Column.ValidationValue" Mode="OneWay" /&gt; &lt;/DataTrigger.Binding&gt; &lt;Setter Property="BorderBrush" Value="{StaticResource errorBrush}" /&gt; &lt;/DataTrigger&gt; </code></pre> <p>I've defined the Attached Property in my DataGrid class (which is named ValidatingDataGrid and extends the DataGrid) as follows:</p> <pre><code>public static readonly DependencyProperty ValidationValueProperty = DependencyProperty.RegisterAttached("ValidationValue", typeof(object), typeof(DataGridColumn)); public static object GetValidationValue(DependencyObject element) { return element.GetValue(ValidationValueProperty); } public static void SetValidationValue(DependencyObject element, object value) { element.SetValue(ValidationValueProperty, value); } </code></pre> <p>Finally, in my WPF Page, I have a DataGridTextColumn, where I attempt to bind ValidationValue (the Column's AttachedProperty) to a MultiBinding.</p> <pre><code>&lt;vfc:ValidatingDataGrid&gt; &lt;vfc:ValidatingDataGrid.Columns&gt; &lt;tk:DataGridTextColumn Header="Name" Width="1.5*"&gt; &lt;tk:DataGridTextColumn.Binding&gt; &lt;Binding Path="Name" /&gt; &lt;/tk:DataGridTextColumn.Binding&gt; &lt;vfc:ValidatingDataGrid.ValidationValue&gt; &lt;MultiBinding Converter="{StaticResource validityConverter}" ConverterParameter="Name"&gt; &lt;Binding Mode="OneWay" /&gt; &lt;Binding Path="Name" UpdateSourceTrigger="PropertyChanged" /&gt; &lt;/MultiBinding&gt; &lt;/vfc:ValidatingDataGrid.ValidationValue&gt; </code></pre> <p>When I try to run this, however, I consistently get a XAML Parse Exception:</p> <blockquote> <p>System.Windows.Markup.XamlParseException occurred</p> <p>Message="A 'MultiBinding' cannot be set on the 'SetValidationValue' property of type 'DataGridTextColumn'. A 'MultiBinding' can only be set on a DependencyProperty of a DependencyObject."</p> <p>Source="PresentationFramework"</p> <p>LineNumber=0</p> <p>LinePosition=0</p> <p>StackTrace: at MS.Internal.Helper.CheckCanReceiveMarkupExtension(MarkupExtension markupExtension, IProvideValueTarget provideValueTarget, DependencyObject&amp; targetDependencyObject, DependencyProperty&amp; targetDependencyProperty)</p> <p>InnerException: Null</p> </blockquote> <p>I know that if I set ValidationValue to a static value (for example, Error), the value is properly stored and accessed in the DataTrigger. </p> <ol> <li><p>Can anyone explain what the problem actually is? I don't understand what the exception message means since the AttachedProperty is a DependencyProperty, and DataGrid and DataGridColumn are both DependencyObjects. </p></li> <li><p>Is there a way to bind the ValidationValue AttachedProperty to the MultiBinding? If not, is there some other WPF mechanism that will let me store the result of a binding so my DataGridCell style can read it?</p></li> </ol>
 

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