绑定的ElementName一个DataTemplate内 [英] Binding ElementName inside a DataTemplate

查看:123
本文介绍了绑定的ElementName一个DataTemplate内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个属性是依赖于相同的DataTemplate



要在控件绑定说明:

 <&DataTemplate的GT; 
< StackPanel的方向=横向>
<组合框X:NAME =ComboList
的ItemsSource ={结合StatTypes}
的SelectedItem ={结合SelectedStatType,模式=双向,FallbackValue ='选择类型'} >
< ComboBox.ItemTemplate>
<&DataTemplate的GT;
< TextBlock的文本={结合文字}/>
< / DataTemplate中>
< /ComboBox.ItemTemplate>
< /组合框>

<文本框Grid.Column =1了minWidth =40保证金=5>
< TextBox.Text>
<绑定路径=StatValue>
< Binding.Converter>
<变频器:PercentageConverter SelectedStatType ={绑定的ElementName = ComboList,路径=的SelectedItem}/>
< /Binding.Converter>
< /绑定>
< /TextBox.Text>
< /文本框>
< / StackPanel的>
< / DataTemplate中>



但在财产 PercentageConverter 从不通过这样设置,我不明白为什么。这是一个命名范围的问题?如果是这样,我想这不会有问题,因为它是在同一个的DataTemplate
。如果没有,我究竟做错了什么?


解决方案

这可能是一个名称范围问题,结合不是一个框架元素,它里面的任何物体不会分享外面名称范围,也不是在任何树的绑定,所以相对于源绑定应该也失败。



您可以尝试使用的 X:参考 相反,它使用不同的机制:



<预类=郎XML prettyprint-覆盖> {结合的SelectedItem源= {X:参考ComboList}}


I am trying to bind a property that is dependent on a control within the same DataTemplate.

To illustrate:

<DataTemplate>
    <StackPanel Orientation="Horizontal">
        <ComboBox x:Name="ComboList"
                  ItemsSource="{Binding StatTypes}"
                  SelectedItem="{Binding SelectedStatType, Mode=TwoWay, FallbackValue='Select a type'}">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Text}"/>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>

        <TextBox Grid.Column="1" MinWidth="40" Margin="5">
            <TextBox.Text>
                <Binding Path="StatValue">
                    <Binding.Converter>
                        <converter:PercentageConverter SelectedStatType="{Binding ElementName=ComboList, Path=SelectedItem}" />
                    </Binding.Converter>
                </Binding>
            </TextBox.Text>
        </TextBox>
    </StackPanel>
</DataTemplate>

But the property in the PercentageConverter is never set through this and I don't see why. Is this a naming scope issue? If so, I thought this would not matter since it is in the same DataTemplate If not, what am I doing wrong?

解决方案

This is probably a namescope issue, the binding is not a framework element, any objects inside it will not share the outside namescope, nor is the binding in any tree, so relative source bindings should fail as well.

You can try using x:Reference instead, it uses a different mechanism:

{Binding SelectedItem, Source={x:Reference ComboList}}

这篇关于绑定的ElementName一个DataTemplate内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆