user3306382
user3306382

Reputation: 1

Set property of ComboBox within UserControl instance

I have a UserControl that contains 2 ComboBoxes and 2 TextBoxes. I have 2 instances of the UserControl in the MainWindow.

The Visibility of the second instance of the UserControl is binded to CheckBox which is placed in the MainWindow - the UserControl becomes Hidden/Visible depending on the CheckBox.IsChecked property.

The IsChecked and the IsEnabled properties of the CheckBox itself are binded to the SelectedItem property of the first ComboBox in the first instance of the UserControl - such as: if the value is null they will be false. I'm using MVVM so this binding is defined on the ViewModel part.

My question is - is there a way to access the SelectedIndex property of one of the ComboBox of the UserControl through the XAML in the MainWindow, so when the CheckBox is UnChecked, except that the second instance of the UserControl will turn to Hidden, the SelectedIndex of the ComboBox within the second instance will turn to 0?

The UserControl XAML:

<ComboBox x:Name="comboField" ItemsSource="{Binding Fields}"
          DisplayMemberPath="Key"
          SelectedItem="{Binding FieldComboSelectedItem, Mode=TwoWay}"/>
<TextBox x:Name="tbFirstValue"/>
<ComboBox x:Name="comboQueryType"
          SelectedItem="{Binding QueryTypeComboSelectedItem, Mode=TwoWay}"/>
<TextBox x:Name="tbSecondValue"/>

The 2 instances and the CheckBox in the MainWindow XAML:

<local:FilterUC x:Name="firstFilter"/>    
<local:FilterUC x:Name="secondFilter">
    <local:FilterUC.Style>
        <Style TargetType="{x:Type local:FilterUC}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsChecked,
                               ElementName=cbTwoFields}" Value="False">
                     <Setter Property="Visibility" Value="Hidden"/>
                                 <!--This is what I want to achieve:-->
                     <!--<Setter ElementName="comboField"
                                 Property="SelectedIndex" Value="0" />-->
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </local:FilterUC.Style>
</local:FilterUC>
<CheckBox x:Name="cbTwoFields" Content="Add filter" 
          IsEnabled="{Binding ElementName=firstFilter, 
                              Path=DataContext.AddFilterEnable}" 
          IsChecked="{Binding ElementName=firstFilter, 
                              Path=DataContext.AddFilterChecked}"/>

Upvotes: 0

Views: 989

Answers (1)

Sheridan
Sheridan

Reputation: 69959

You could always just add a DependencyProperty to expose the ComboBox.SelectedIndex property:

public static DependencyProperty SelectedIndexProperty = DependencyProperty.Register(
    "SelectedIndex", typeof(int), typeof(FilterUC), new PropertyMetadata(0));

public int SelectedIndex
{
    get { return (int)GetValue(SelectedIndexProperty); }
    set { SetValue(SelectedIndexProperty, value); }
}

You didn't say which ComboBox you wanted to reset, so I'll just use this one to demonstrate how you should bind the ComboBox.SelectedIndex property to the new DependencyProperty using a RelativeSource Binding:

<ComboBox x:Name="comboQueryType" SelectedIndex="{Binding SelectedIndex,
    RelativeSource={RelativeSource AncestorType={x:Type local:FilterUC}}}"
    SelectedItem="{Binding QueryTypeComboSelectedItem, Mode=TwoWay}" />

Then you should be able to access it in the DataTrigger. Try this:

<Style TargetType="{x:Type local:FilterUC}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding IsChecked,
                       ElementName=cbTwoFields}" Value="False">
             <Setter Property="Visibility" Value="Hidden"/>
             <Setter Property="SelectedIndex" Value="0" />
        </DataTrigger>
    </Style.Triggers>
</Style>

Upvotes: 1

Related Questions