开发者

Set to visible when item in a combobox is selected

开发者 https://www.devze.com 2023-03-19 15:51 出处:网络
As the title says, I have a hidden border with some controls inside, and I would like to show it when a particular item in a combobox is selected.

As the title says, I have a hidden border with some controls inside, and I would like to show it when a particular item in a combobox is selected.

I tried the following

<ComboBox Name="cmbRequiredRule" SelectedValuePath="Content"                                                                                                    
          SelectedValue="{Binding Path=ClientValidation.NarrativeRequiredRule}">
    <ComboBoxItem>All</ComboBoxItem>                         
    <ComboBoxItem>Matching</ComboBoxItem>
</ComboBox>

<Border Visibility="Collapsed">
    <Border.Resources>
        <Style TargetType="{x:Type Border}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=ClientValidation.NarrativeRequiredRule}" Value="Matching">
                    <Setter Property="Visibility" Value="Visible" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Border.Resources> 
.... 
</Border>

and this property in the view model:

 public string NarrativeRequiredRule
    {
        get... 
        set...开发者_运维问答 
    } 

but the trigger doesn't seem to be working


Try setting Visibility=Collapsed in your Style Setters, not as part of the Border Tag. I've had issues in the past where a DataTrigger would not apply when the value was specified as part of the Tag.

    <Border>
        <Border.Resources>
            <Style TargetType="{x:Type Border}">
                <Setter Property="Visibility" Value="Collapsed" />
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Path=Test}" Value="Matching">
                        <Setter Property="Visibility" Value="Visible" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Border.Resources>
        ...
    </Border>


Locally assigned value takes precedence over styles. Hence you need to have

<Setter Property="Visibility" Value="Collapsed" />

in Style as @Rachel has pointed out. Also I tried debugging the binding using a dummy converter and found that the value turned out to be System.Windows.Controls.ComboBoxItem: Matching instead of Matching.

Hence your final style is:

            <Style TargetType="{x:Type Border}">
                <Setter Property="Visibility" Value="Collapsed" />
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Path=ClientValidation.NarrativeRequiredRule}" Value="System.Windows.Controls.ComboBoxItem: Matching">
                        <Setter Property="Visibility" Value="Visible" />
                    </DataTrigger> 
                </Style.Triggers>                    
            </Style>


could be binding issue. In your example below:

<DataTrigger Binding="{Binding Path=ClientValidation.NarrativeRequiredRule}" Value="Matching">

where is the ClientValidation located ? because if the whole View's DataContext is bound to VM, you will need to include these hierarchies. Check your Output log, it should throw some errors if binding failes

0

精彩评论

暂无评论...
验证码 换一张
取 消