WPF:如果用户单击子列表框,如何选择父列表框

时间:2010-02-17 06:57:11

标签: c# wpf binding listbox datatemplate

在我使用DataTemplate机制的应用程序中,我将另一个列表框插入到ListBox项目中。但有可能在父列表框中选择一个listboxitem时,焦点可能在另一个父listboxitem的孩子身上(见图片)

Pic http://i053.radikal.ru/1002/fc/3e68c57e3488.png

怎么做:如果其中一个子列表框处于焦点(选择了一个项目),那么选择父列表框?使用绑定或模板

<DataTemplate x:Key="NotesListBoxDataTemplate" DataType="Note">
    <StackPanel Orientation="Vertical">

        <StackPanel Orientation="Horizontal">
            <TextBox Text="{Binding Path=Title, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox>
            <my:DatePicker Height="25" Name="datePicker1" Width="115" xmlns:my="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit" 
                           SelectedDate="{Binding LastEdit,
                                                  Mode = TwoWay}" />
        </StackPanel>
        <TextBox Text="{Binding Path=Content, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox>

        <StackPanel Orientation="Horizontal">
            <ListBox Name="ImagesListBox"  SelectedIndex="{Binding Mode=OneWayToSource, Source={StaticResource progParameters}, Path=SelectedImage, UpdateSourceTrigger=PropertyChanged}" ItemsSource="{Binding Path=Images}" ItemTemplate="{StaticResource NotesListBoxImagesTemplate}" Style="{StaticResource HorizontalListBox}">
            </ListBox>
            <StackPanel Orientation="Vertical">
                <Button Name="AddImageButon" Content="+" Click="AddImageButon_Click"></Button>
                <Button Name="RemoveImageButon" Content="-" Click="RemoveImageButon_Click"></Button>
            </StackPanel>

        </StackPanel>

    </StackPanel>
</DataTemplate>

1 个答案:

答案 0 :(得分:3)

在父ListBox设置属性IsSynchronizedWithCurrentItemtrue上,然后在内ListBox es中将SelectedItem属性设置为"{Binding SelectedItem ElementName=lbParent}"

考虑使用Converter来帮助您获取无法通过xaml访问的特定数据,或者是否需要进行一些计算。

希望这有帮助。

相关问题