我目前有一个ListView,它接收一个项目并显示一个ComboBox和一个Button .
我想根据ComboBox的值等于“BlahBlahBlah”动态显示或隐藏按钮 .
目前我的 <GridView>
内部看起来像这样:
<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>
谁能指出我哪里出错了?我得到的只是:
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')
1 回答
您已经将 SelectedValue 属性绑定到源类,因此 bind directly to that property 而不是ComboBox . 问题是ComboBox和Button位于不同的Visual树中,因此无法使用ElementName进行绑定 .