Veronica
Veronica

Reputation: 23

XAML Binding inside Path=DataContext Binding

sorry if the title of the question is not really clear, but I wasn't sure on how to phrase it.

I have a dependency property called ComboBoxContent. The setup of the dp is alright so I don't post the code.

The value is set as follows:

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

Inside the usercontrol UcCbMultiselect1, is a label which binds to the dp:

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

If the Label would bind to Path=DataContext.Sprache.Bezeichnung , it would work properly. But as I need to reuse the usercontrol I can't do that.

Is there any way to realize it? Its like I want to have the following binding (which obviously does not work):

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

Thanks for your help


EDIT

<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}"
                        />

And this is how the usercontrol looks like (tried to take only the part that seemed to be important)

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

Upvotes: 0

Views: 249

Answers (1)

lightlike
lightlike

Reputation: 162

You do not want to work with the ListBoxItem. The Control contains the DependencyProperty, right?

Try this:

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

Upvotes: 0

Related Questions