【问题标题】:XAML Binding inside Path=DataContext BindingPath=DataContext 绑定中的 XAML 绑定
【发布时间】: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

标签: xaml binding


【解决方案1】:

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

试试这个:

{Binding ComboBoxContent, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}

【讨论】:

  • 谢谢lightlike,不幸的是它有点复杂,我将编辑我的问题并发布另一个代码示例
  • 你试过没有Path中DataContext的Binding吗?
  • 是的,但是如果没有数据上下文,它会尝试绑定到组合框的数据上下文,而不是组合框中列表框的数据上下文。如果我将要在 dp 'ComboBoxContent' 中传递的值直接写入绑定表达式,它将显示正确的值。但由于它需要可重复使用,我不能这样做
  • 也许this 会有所帮助。如果没有,我真的不知道会发生什么。
猜你喜欢
  • 2014-03-09
  • 1970-01-01
  • 2013-02-19
  • 2012-10-14
  • 2016-03-10
  • 2013-04-16
  • 2015-10-22
  • 2010-11-23
  • 1970-01-01
相关资源
最近更新 更多