1

I have a ComboBox that has an ItemTemplate with a CheckBox in it (read Dropdown CheckBoxList). I basically want to have a separate binding for the text in the combobox (both when an item is selected, and if an item isn't selected). I've tried a few different methods but either can't seem to get them to work with my setup, or can't find they have some issues. I've even tried a few mixes and matches of them. Usually I find they either don't work when an item isn't specifically selected (which could be possible if the user checks a few boxes then clicks out of the ComboBox), or aren't updating the text properly.

My xaml is this (the items of focus are the DataTemplates with the ComboBoxes in them):

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

Ideally, I'd like to bind the display text of the combobox with the OfficeListValues method in my ViewModel.

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

Any help would be appreciated!

Community
  • 1
  • 1
jmgardn2
  • 981
  • 2
  • 8
  • 21

0 Answers0