开发者

Binding with a converter inside a template

开发者 https://www.devze.com 2023-03-25 03:39 出处:网络
I have defined a DataTemplate for a ListBox.Inside the template, I use TextBlock controls to display the properties of the data context.For example:

I have defined a DataTemplate for a ListBox. Inside the template, I use TextBlock controls to display the properties of the data context. For example:

<TextBlock Text="{Binding Path=FirstName}" />

And if I do this:

<TextBlock Visibility="{Binding Path=IsAccountValid}" />

...the application runs, but there is a warning in the output about trying to bind a boolean property to a Visibility enumeration.

If I do this:

<TextBlock Visibility="{Binding Path=IsAccountValid,Converter={StaticResource visibilityOfBool}}" />

and somewhere in my App.xaml is:

<BooleanToVisibilityConverter x:Key="visibilityOfBool" />

I get a null reference exception.

I suspected this might be because the property IsAccountValid is not a dependency property, so I added a CheckBox to the window, and did this:

<TextBlock Visibility="{Binding Path=IsChecked,Converter={StaticResource visibilityOfBool},开发者_开发问答ElementName=butA}" />

But got the same error.

Why? The DataContext object is valid because if I bind IsAccountValid to the Text property, the value is correctly displayed.

The converter is never called, so I am wondering if it is the converter that cannot be found.

Why can the converter not be found? Why can the converter be found outside the data template and not inside the data template?

I tried building the template again with Blend, as Blend usually gets it right, but the code it generated was the same as mine.

I tried some of the fixes suggested on this website, including setting RelativeSource to TemplateParent and Self, but it made no difference.

What is going on?


We use such converter all the time in our Data Templates. Do you define the converter key inside your resource dictionary? Merge another Resource Dictionary?


The IsAccountValid property doesn't have to be a dependency property. If the converter couldn't be found then you wouldn't be able to open the form. You have the right approach using the converter but it is difficult to say exactly what is causing the exception without seeing more information.


As Amittai and Chris pointed out, it seems that you're headed in the right direction. I know it sounds a bit stupid, but try to add a space between the comma and the Converter= statement in the binding. Like so:

<TextBlock Visibility="{Binding Path=IsAccountValid, Converter={StaticResource visibilityOfBool}}" />

On some systems there are weird symptoms when there's no space after the comma. I couldn't find the actual reason for that.


Thank you all for your help in my investigation.

I have solved the problem.

These two lines of code are included in the DataTemplate, one is a TextBlock, and one is a hyperlink:

<TextBlock Text="Hello" Visibility="{Binding IsChecked,ElementName=chkBox,Converter={StaticResource visibilityOfBool}}" />

and

<TextBlock Grid.Column="1" >
    <Hyperlink Click="ProgHomePageHyperlink_Click" >
         <TextBlock Text="{Binding Path=Title}" />
    </Hyperlink>
</TextBlock>

When they are both included in the code, the runtime throws a null reference exception.

But if I comment one of them out, either the TextBlock or the HyperLink, everything runs ok.

If I remove the Click handler from the hyperlink, everything runs ok.

If I comment out the converter in the TextBlock, the application runs, but I get a mismatched binding warning, which is well deserved.

So, including a Click handler in the hyperlink means the converter in the TextBlock cannot be found.

How weird is that!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号