Path中的XAML绑定= DataContext绑定

时间:2017-07-21 10:04:39

标签: xaml binding

抱歉,如果问题的标题不是很清楚,但我不确定如何说出来。

我有一个名为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"
                        />

在usercontrol UcCbMultiselect1中,是一个绑定到dp的标签:

 <Label Content="{Binding Path=DataContext.ComboBoxContent, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}"/>

如果Label绑定到Path = DataContext.Sprache.Bezeichnung,它将正常工作。但是因为我需要重用usercontrol,所以我无法做到这一点。

有没有办法实现它?它就像我想要有以下绑定(显然不起作用):

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

1 个答案:

答案 0 :(得分:0)

您不想使用ListBoxItem。 Control包含DependencyProperty,对吗?

试试这个:

{Binding ComboBoxContent, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}
相关问题