简体   繁体   中英

How to bind selected Item from ListBox to ComboBox

I have a ListBox and items are generated dynamically. I also have a ComboBox .

Each item has a string property called 'Name'.

When I select this item on ListBox , I want to set default value or text of ComboBox with 'Name' of selected item from ListBox .

I could bind this with 'Text' property of 'TextBlock'. But I couldn't do it with ComboBox

Here's my code:

XAML

<ListBox Name="MyList" ItemsSource="{Binding SourceList}"
         SelectedItem="{Binding SelectedObject, Mode=TwoWay}">
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel Width="{Binding ElementName=MyList, Path=ActualWidth}"/>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Border>
                <StackPanel>                   
                    <Grid>
                        <StackPanel>
                            <TextBlock {Binding Path=FileName}" />    
                            <TextBlock>
                                <Run Text="{Binding Name}" />
                            </TextBlock> 
                        </StackPanel>
                    </Grid>
                </StackPanel>
            </Border>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

I could bind this TextBlock :

<TextBlock>
   <Run Text="{Binding SelectedItem.Name, ElementName=MyList}"/>
</TextBlock>

But it doesn't work with ComboBox :

<ComboBox ItemsSource="{Binding ElementName=MyList, Path=SelectedItem}" SelectedItem="{Binding SelectedObject}" Text="{Binding Name}">                             
</ComboBox>

You set the ItemsSource property on ComboBox to the SelectedItem in your ListBox . This does not work, as a bound items source must be a collection, in your case it is a single non-collection item.

Let us assume that you have bound the ItemsSource to a collection, eg SourceList . Then you can use the DisplayMemberPath property to specify the property that should be used as label in the ComboBox , here Name .

<ComboBox ItemsSource="{Binding SourceList}"
          SelectedItem="{Binding SelectedItem, ElementName=MyList}"
          DisplayMemberPath="Name">

If you want to set the default value, you can use the Text property, but you also have to set the IsEditable property to true , otherwise it will be ignored. From the documentation :

When the IsEditable property is true, setting this property places initial text entered in the text box. When IsEditable is false, setting this value has no effect.

The downside to this is, that there will be a default text, but the ComboBox value can be edited.

<ComboBox ItemsSource="{Binding SourceList}"
          SelectedItem="{Binding SelectedItem, ElementName=MyList}"
          DisplayMemberPath="Name"
          IsEditable="True">

If editing is an issue with your requirements, you can have a look at this related question . There are workarounds, but they are quite complex and have some disadvantages, too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM