0

I have a custom dependency property that I would like to bind to and pass into a value converter within a style. Here is the code:

   <Window.Resources>
      <local:Conv x:Key="MyConv" />

      <Style x:Key="TextBoxField" TargetType="{x:Type TextBox}">
         <Setter Property="DataContext">
            <Setter.Value>
               <MultiBinding Converter="{StaticResource MyConv}">
                  <Binding RelativeSource="{RelativeSource AncestorType={x:Type Window}}" />
                  <Binding Path="local:FieldName.FieldId" RelativeSource="{RelativeSource AncestorType={x:Type TextBox}}" />
               </MultiBinding>
            </Setter.Value>
         </Setter>
      </Style>
   </Window.Resources>
   <Grid>
      <TextBox local:FieldName.FieldId="FirstName" Style="{StaticResource TextBoxField}"/>
   </Grid>

I have set my custom dependency property FieldId on the TextBox. In the style, I am trying to reference it to pass into my converter. The Window passes in correctly, but FieldId comes in as an Unset value.

I have tried various RelativeSource's, referencing "local" or not "local" while referencing the custom property, etc.

2
  • FieldName.FieldId appears to be an attached property, which must be put in parentheses in a Binding Path, i.e. Path="(local:FieldName.FieldId)". See PropertyPath for Objects in Data Binding. Commented Jan 26 at 22:48
  • Yes, FieldName.FieldId is an attached property. I changed the code, wrapping it in parentheses as you have done, but the result didn't change. I'm still not getting FieldId passed into the converter function. I also tried removing the RelativeSource reference and that didn't make a difference either. Commented Jan 27 at 4:23

1 Answer 1

0

Okay, I stumbled (emphasis on "stumbled") on the answer.

I named the TextBox control "Test" and added "ElementName="Test"" to the FieldId line in the style and it worked. So I knew there was some way to accomplish this.

I removed "Test", putting the Style back the way it was. Then I changed the line to the following:

<Binding Path="(local:FieldName.FieldId)" RelativeSource="{RelativeSource Self}" />

The control's attached prophet FieldId is now correctly passed into the converter.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.