Mir Sabbir
Mir Sabbir

Reputation: 33

WPF, Caliburn.micro : How to bind property outside of current binding context?

<DataTemplate>
<StackPanel Orientation="Vertical">
    <TextBlock Text="{Binding Key}"></TextBlock>
    <ComboBox ItemsSource="{Binding Value}" SelectedItem="{Binging SomeProperty}">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Name}"></TextBlock>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>
</StackPanel>

I am inside a Dictionary , I want to bind SelectedItem="{Binging SomeProperty}" SomeProperty is a property of the ViweModel, not inside of the Dictionary. How to do that? How can I bind to properties outside of current binding context.

Upvotes: 0

Views: 142

Answers (1)

aepot
aepot

Reputation: 4824

In case of Binding to the Window.DataContext's property, you may do it the following way with RelativeSource:

<ComboBox ItemsSource="{Binding Value}" SelectedItem="{Binding DataContext.SomeProperty, RelativeSource={RelativeSource AncestorType=Window}}">

Or in case you want to display SelectedItem for example in some TextBox (for example collection of string):

<TextBox Text="{Binding MyCollection/}"/>
<ComboBox ItemsSource="{Binding MyCollection}" IsSynchronizedWithCurrentItem="True">

MyCollection with "/" gets the current item of the ICollectionView used as DefaultView for MyCollection. Read more >>>

Upvotes: 1

Related Questions