带CheckBox的WPF ComboBox - 折叠显示值

时间:2013-06-04 17:19:48

标签: c# wpf binding combobox

我的ComboBox有一个ItemTemplate,其中包含CheckBox(读取Dropdown CheckBoxList)。我基本上想要对组合框中的文本进行单独绑定(当选择项目时,以及未选择项目时)。我已经tried few different methods,但要么无法让他们使用我的设置,要么找不到他们有一些问题。我甚至尝试过一些混音和火柴。通常我发现当没有特别选择项目时它们不起作用(如果用户检查了几个框然后点击了ComboBox),或者没有正确更新文本,这可能是可行的。

我的xaml就是这个(焦点项是DataTemplates,其中包含ComboBoxes

<UserControl x:Class="BestClient.ucReportViewer"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:BestClient"
         mc:Ignorable="d" 
         d:DesignHeight="315" d:DesignWidth="388" Background="#FFD7DFEC">

<UserControl.Resources>
    <DataTemplate x:Key="datetime">
        <StackPanel Orientation="Horizontal" Height="35">
            <TextBlock Text="{Binding rfName}" Width="100" VerticalAlignment="Center" />
            <DatePicker Tag="{Binding rfParameter}" SelectedDate="{Binding rfValues, Mode=TwoWay}" Width="200" VerticalAlignment="Center" />
        </StackPanel>
    </DataTemplate>
<DataTemplate x:Key="office">
    <StackPanel Orientation="Horizontal" Height="35">
        <TextBlock Text="{Binding rfName}" Width="100" VerticalAlignment="Center" />
        <ComboBox x:Name="officeComboBox" Tag="{Binding rfParameter}" Width="200" ItemsSource="{Binding Path=DataContext.OfficeList, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Mode=TwoWay}" VerticalAlignment="Center">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <CheckBox Tag="{Binding id}" IsChecked="{Binding isSelected}" Width="200" Content="{Binding value}" Margin="2" x:Name="CheckBox" Checked="OfficeCheckBox_Checked"/>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>
    </StackPanel>
</DataTemplate>
    <DataTemplate x:Key="carrier">
        <StackPanel Orientation="Horizontal" Height="35">
            <TextBlock Text="{Binding rfName}" Width="100" VerticalAlignment="Center" />
            <ComboBox Tag="{Binding rfParameter}" Width="200" ItemsSource="{Binding Path=DataContext.CarrierList, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Mode=TwoWay}" VerticalAlignment="Center">
                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <CheckBox Tag="{Binding id}" IsChecked="{Binding isSelected}" Width="200" Content="{Binding value}" Margin="2"/>
                        </StackPanel>
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>
        </StackPanel>
    </DataTemplate>
    <DataTemplate x:Key="defaultTemplate">
        <StackPanel Orientation="Horizontal" Height="35">
            <TextBlock Text="{Binding rfName}" Width="100" VerticalAlignment="Center" />
            <TextBox Tag="{Binding rfParameter}" Width="200" VerticalAlignment="Center" />
        </StackPanel>
    </DataTemplate>
    <local:ReportFilterTemplateSelector x:Key="ReportFilterTemplateSelector" />
</UserControl.Resources>
<Grid x:Name="ReportViewer">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="1*"/>
        <ColumnDefinition Width="1*"/>
        <ColumnDefinition Width="1*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="45"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="45"/>
    </Grid.RowDefinitions>
    <Viewbox Grid.Row="0" Grid.Column="0">
        <TextBlock Margin="10" TextWrapping="Wrap" Text="Select a Report:"/>
    </Viewbox>
    <ComboBox Grid.Column="1" Margin="10" Grid.ColumnSpan="2" ItemsSource="{Binding ReportList}" DisplayMemberPath="Value" SelectedValuePath="Key" SelectedItem="{Binding SelectedReport}" VerticalContentAlignment="Center" />
    <ListView x:Name="FilterList" Margin="10,0" Grid.Row="1" Grid.ColumnSpan="3" ItemTemplateSelector="{StaticResource ReportFilterTemplateSelector}" ItemsSource="{Binding reportFilters, Mode=TwoWay}" />
    <Button Command="{Binding OpenReport}" Content="Open Report" Grid.Column="2" Margin="10" Grid.Row="2" VerticalAlignment="Stretch"/>
</Grid>
</UserControl>

理想情况下,我想在我的ViewModel中使用OfficeListValues方法绑定组合框的显示文本。

{Binding Path=DataContext.OfficeListValues, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}

任何帮助将不胜感激!

0 个答案:

没有答案