开发者

Wpf Combobox selectedvalue trigger

开发者 https://www.devze.com 2023-03-11 06:48 出处:网络
I am trying to change the visibility with a trigger when a particular value in a combobox is selected, and I got the following XAML

I am trying to change the visibility with a trigger when a particular value in a combobox is selected, and I got the following XAML

 <ItemsControl ItemsSource="{Binding AccessControl.Credentials}" >
                        <ItemsControl.ItemTemplate>
                        <DataTemplate>                              
                            <Grid  >                                  

                             <ComboBox  Name="chkFieldType"                                          
                                        SelectedValue="{Binding Path=ValueSourceType,Converter={StaticResource enumstringConv}}"
                                        SelectedValuePath="Tag" SelectionChanged="chkFieldType_SelectionChanged" >
                              开发者_开发问答             <ComboBoxItem Tag="User">User</ComboBoxItem>                                     
                                           <ComboBoxItem Tag="SessionCredential">Field</ComboBoxItem>
                                           <ComboBoxItem Tag="Inherit">From other Resource</ComboBoxItem>
                             </ComboBox>


                                    <Border " Visibility="Hidden">
                                        <Border.Resources>
                                            <Style TargetType="{x:Type Border}">
                                                <Style.Triggers>
                                                    <DataTrigger Binding="{Binding Path=SelectedValue, ElementName=chkFieldType}" Value="Inherit">
                                                        <Setter Property="Visibility" Value="Visible" />
                                                    </DataTrigger>
                                                </Style.Triggers>
                                            </Style>
                                        </Border.Resources>
                                        <ComboBox/>                                         
                                    </Border>

In this case a border. The selected value is "Inherit" of type string but the border remainds hidden.


I ran into the same problem and found that you have to set the visibility property using the style only. So instead of having the initial visibility set with:

<Border Visibility="Hidden">

You should set the initial visibility using the style:

<Style TargetType="....">
   <Setter Property="Visibility" Value="Hidden"/>
   <Style.Triggers>
      ....
   </Style.Triggers>
</Style>

(I know it's kinda overdue but I thought maybe someone else might run into the same problem).


Try SelectedItem.Tag or SelectedItem.Content instead of SelectedValue


Set your binding on SelectedValue, not SelectedItem.SelectedValue. The way you currently have it, it is looking for ComboBoxItem.SelectedValue, which doesn't exist

<DataTrigger Value="Inherit" 
             Binding="{Binding Path=SelectedValue,
                               Converter={StaticResource enumstringConv}, 
                               ElementName=chkFieldType}">


I think it's because you are putting the DataTrigger in Border.Resources. Try putting the style in the window.resources, with a x:key in order to apply the style to the border.

I think that the border.resources can not access to a control "outside it's own resources context"


SelectedItem and SelectedValue are two seperate properties on the ComboBox. Since your ComboBoxItems are all strings you can change
<DataTrigger Binding="{Binding Path=SelectedItem.SelectedValue, ElementName=chkFieldType}" Value="Inherit">
to
<DataTrigger Binding="{Binding Path=SelectedItem, ElementName=chkFieldType}" Value="Inherit">


I ended up setting the visibility manually via the code behind when the selectedItem event is fired..

0

精彩评论

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

关注公众号