路径内的XAML绑定=数据上下文绑定
对不起,如果问题的标题不是很清楚,但我不知道该如何表达 我有一个名为ComboBoxContent的依赖属性。dp的设置是正确的,所以我不发布代码 该值设置如下:路径内的XAML绑定=数据上下文绑定,xaml,binding,Xaml,Binding,对不起,如果问题的标题不是很清楚,但我不知道该如何表达 我有一个名为ComboBoxContent的依赖属性。dp的设置是正确的,所以我不发布代码 该值设置如下: <userControls:UcCbMultiselect1 ItemsSource="{Binding OcSprache, UpdateSourceTrigger=PropertyChanged}" CbMultiSelectI
<userControls:UcCbMultiselect1
ItemsSource="{Binding OcSprache, UpdateSourceTrigger=PropertyChanged}"
CbMultiSelectItemTemplate="{DynamicResource UcCbMultiselectSprache2Land}"
SelectedItems="{Binding CurrentItemMainListBox.ListZtSprache2Land, UpdateSourceTrigger=PropertyChanged}"
DeleteButtonVisibility="{Binding Editierbar, Converter={StaticResource Boolean2VisibilityConverter}}"
ComboBoxContent="Sprache.Bezeichnung"
/>
在usercontrol UcCbMultiselect1中,有一个绑定到dp的标签:
<Label Content="{Binding Path=DataContext.ComboBoxContent, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}"/>
如果标签将绑定到Path=DataContext.Sprache.Bezeichnung,它将正常工作。但是由于我需要重用usercontrol,所以我不能这样做
有没有办法实现这一点?就像我想要有以下绑定(显然不起作用):
谢谢你的帮助
编辑
<userControls:UcCbMultiselect1
ItemsSource="{Binding OcSprache, UpdateSourceTrigger=PropertyChanged}"
CbMultiSelectItemTemplate="{DynamicResource UcCbMultiselectSprache2Land}"
SelectedItems="{Binding CurrentItemMainListBox.ListZtSprache2Land, UpdateSourceTrigger=PropertyChanged}"
DeleteButtonVisibility="{Binding Editierbar, Converter={StaticResource Boolean2VisibilityConverter}}"
ComboBoxContent="{Binding Sprache.Bezeichnung}"
/>
这就是usercontrol看起来的样子(试图只接受看起来很重要的部分)
...
您不想使用ListBoxItem。控件包含DependencyProperty,对吗
试试这个:
{Binding ComboBoxContent, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}
您不想使用ListBoxItem。控件包含DependencyProperty,对吗 试试这个:
{Binding ComboBoxContent, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}
谢谢lightlike,这有点复杂。不幸的是,我将编辑我的问题并发布另一个代码示例。您是否尝试了路径中没有DataContext的绑定?是的,但如果没有DataContext,它会尝试绑定到combobox的DataContext,而不是combobox中listbox的DataContext。如果我将要在dp'ComboBoxContent'内部传递的值直接写入绑定表达式,它将显示正确的值。但由于它需要可重复使用,我不能这样做,也许会有所帮助。如果没有,我真的不知道会发生什么。谢谢lightlike,这有点复杂。不幸的是,我将编辑我的问题并发布另一个代码示例。您是否尝试了路径中没有DataContext的绑定?是的,但如果没有DataContext,它会尝试绑定到combobox的DataContext,而不是组合框内列表框的datacontext。如果我将要在dp'ComboBoxContent'内部传递的值直接写入绑定表达式,它将显示正确的值。但由于它需要可重复使用,我不能这样做,也许会有所帮助。如果没有,我真的不知道会发生什么。在usercontrol的代码隐藏中,您到底在哪个类中定义DependencyProperty?因为它只与这个特定的usercontrol相关。该设置是dps的标准设置。usercontrol中的所有其他dp都在工作-ItemsSource、SelectedItems等。问题是我需要绑定Path=DataContext.comboxcontent。通过这种方式,VS尝试在ViewModel中查找属性ComboBoxContent。在usercontrol的代码隐藏中,您究竟在哪个类中定义DependencyProperty?,因为它仅与此特定usercontrol相关。该设置是dps的标准设置。usercontrol中的所有其他dp都在工作-ItemsSource、SelectedItems等。问题是我需要绑定Path=DataContext.comboxcontent。这样VS会尝试在ViewModel中查找属性ComboBoxContent
{Binding ComboBoxContent, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}