简体   繁体   中英

WPF XAML - How to bind a DataTrigger to a ComboBox value

I currently have a ListView that takes in an item and displays a ComboBox and a Button.

I would like to dynamically show or hide the button based on the value of the ComboBox being equal to "BlahBlahBlah".

Currently the inside of my <GridView> looks like this:

<GridViewColumn Header="Property" Width="160">
  <GridViewColumn.CellTemplate>
      <DataTemplate>
         <ComboBox x:Name="PropertyComboBox"
                  ItemsSource="{Binding Path=ArisingPropertyList, Mode=TwoWay}"
                  SelectedValue="{Binding ArisingProperty.PropertyName,
                  UpdateSourceTrigger=PropertyChanged}"
                  SelectedValuePath="PropertyName" Width="140" >
          </ComboBox>
        </DataTemplate>
   </GridViewColumn.CellTemplate>
</GridViewColumn>                                         


<GridViewColumn Width="30" >                                    
  <GridViewColumn.CellTemplate>                                        
     <DataTemplate>
         <Button Content="...">
            <Button.Style>
               <Style TargetType="{x:Type Button}">
                 <Style.Triggers>
                   <DataTrigger Binding="{Binding Path=SelectedValue.PropertyName, 
                    ElementName=PropertyComboBox}" Value="HideButton">
                        <Setter Property="Visibility" Value="BlahBlahBlah" />
                   </DataTrigger>
                 </Style.Triggers>
              </Style>
           </Button.Style>
        </Button>                                                
     </DataTemplate>
   </GridViewColumn.CellTemplate>
 </GridViewColumn>

Can someone point out where I'm going wrong? All I'm getting is:

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=PropertyComboBox'. BindingExpression:Path=SelectedValue.PropertyName; DataItem=null; target element is 'Button'; target property is 'NoTarget' (type 'Object')

You have already bounded SelectedValue property to your source class, so bind directly to that property instead of ComboBox. Issue is ComboBox and Button lies in different Visual trees so can't be bind using ElementName.

<DataTrigger Binding="{Binding Path=ArisingProperty.PropertyName}"
             Value="HideButton">
   <Setter Property="Visibility" Value="Collapsed" />
</DataTrigger>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM