gpt4 book ai didi

c# - 在 DataTemplate 中绑定(bind) ElementName

转载 作者:IT王子 更新时间:2023-10-29 04:48:56 26 4
gpt4 key购买 nike

我正在尝试绑定(bind)一个依赖于同一 DataTemplate 中控件的属性。

举例说明:

<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>

但是 PercentageConverter 中的属性从未通过此设置,我不明白为什么。这是命名范围问题吗?如果是这样,我认为这无关紧要,因为它在同一个 DataTemplate 中如果不是,我做错了什么?

最佳答案

这可能是名称范围问题,绑定(bind)不是框架元素,其中的任何对象都不会共享外部名称范围,任何树中的绑定(bind)也不会共享,因此相对源绑定(bind)也应该失败。

您可以尝试使用 x:Reference相反,它使用不同的机制:

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

关于c# - 在 DataTemplate 中绑定(bind) ElementName,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11487649/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com