【发布时间】:2017-07-21 19:00:56
【问题描述】:
对不起,如果问题的标题不是很清楚,但我不确定如何表达它。
我有一个名为 ComboBoxContent 的依赖属性。 dp的设置没问题,所以我不发布代码。
值设置如下:
<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"
/>
在用户控件 UcCbMultiselect1 内部,是一个绑定到 dp 的标签:
<Label Content="{Binding Path=DataContext.ComboBoxContent, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}"/>
如果标签将绑定到 Path=DataContext.Sprache.Bezeichnung ,它将正常工作。但是因为我需要重用用户控件,所以我不能这样做。
有什么办法可以实现吗?就像我想要以下绑定(显然不起作用):
<Label Content="{Binding Path=DataContext.{Binding ComboBoxContent}, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}"/>
感谢您的帮助
编辑
<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 x:Class="PassForm.UI.UserControls.UcCbMultiselect1"
x:Name="MultiSelect1"
>
<ComboBox x:Name="CbMultiselect"
DataContext="{Binding}"
ItemsSource="{Binding ItemsSource, ElementName=MultiSelect1}"
ItemTemplate="{Binding CbMultiSelectItemTemplate, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, PresentationTraceSources.TraceLevel=High}"
>
<ComboBox.Template>
<ControlTemplate TargetType="{x:Type ComboBox}">
<Grid Margin="{TemplateBinding Padding}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
UseLayoutRounding="{TemplateBinding UseLayoutRounding}">
<Grid x:Name="InputRoot" HorizontalAlignment="Left">
<ContentPresenter x:Name="contentPresenter"
Content="{TemplateBinding SelectionBoxItem}"
ContentStringFormat="{TemplateBinding SelectionBoxItemStringFormat}"
ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
IsHitTestVisible="False" />
<ListBox x:Name="Present"
DataContext="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:UcCbMultiselect1}}}"
ItemsSource="{Binding SelectedItems, UpdateSourceTrigger=PropertyChanged}"
SelectedItem="{Binding SelectedItem, UpdateSourceTrigger=PropertyChanged}"
>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal" IsItemsHost="True" VerticalAlignment="Bottom"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.Style>
<Style TargetType="{x:Type ListBox}">
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<Border x:Name="PART_BORDER">
<Grid x:Name="Grid">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Label
Content="{Binding Path=DataContext.ComboBoxContent, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}"/>
</Grid>
</Border>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</Setter.Value>
</Setter>
</Style>
</ListBox.Style>
</ListBox>
</Grid>
</Grid>
...
</ControlTemplate>
</ComboBox.Template>
</ComboBox>
【问题讨论】:
-
你到底在哪个类中定义了DependencyProperty?
-
在用户控件后面的代码中,因为它只与这个特定的用户控件有关。该设置是dps的标准设置。用户控件中的所有其他 dps 都在工作 - ItemsSource、SelectedItems 等。问题是我需要绑定 Path=DataContext.ComboBoxContent。这样 VS 会尝试在 ViewModel 中找到属性 ComboBoxContent