将contrentcontrol的内容绑定到组合框的选定值

时间:2013-10-01 08:07:57

标签: wpf binding combobox selectedvalue contentcontrol

我想将contrentcontrol的内容绑定到组合框的选定值,并根据触发器设置可见性。但这不起作用。问题是什么 ?

<DockPanel>
    <ComboBox x:Name="combobo" Padding="4" Width="120" HorizontalAlignment="Left" Margin="30,10,0,0" VerticalAlignment="Center">
        <ComboBoxItem>0</ComboBoxItem>
        <ComboBoxItem>1</ComboBoxItem>
    </ComboBox>
    <ContentControl x:Name="contentcontrol" Grid.Row="0" Grid.Column="2" Content="{Binding ElementName=combobo, Path=SelectedIndex}" Margin="0,10,0,0" VerticalAlignment="Center"
            Visibility="Visible">
        <ContentControl.ContentTemplate>
            <DataTemplate>
                <DockPanel x:Name="WarningGrid" VerticalAlignment="Top" Visibility="Collapsed">
                    <TextBlock Text="Warning" VerticalAlignment="Center"></TextBlock>
                </DockPanel>
                <DataTemplate.Triggers>
                    <Trigger Property="ContentControl.Content" Value="0">
                        <Setter TargetName="WarningGrid" Property="Visibility" Value="Visible" />
                    </Trigger>
                </DataTemplate.Triggers>
            </DataTemplate>
        </ContentControl.ContentTemplate>
    </ContentControl>
</DockPanel>

我也尝试使用SelectedIndex,但触发器也无效。

感谢。

1 个答案:

答案 0 :(得分:0)

将触发器更改为DataTrigger

  <DataTemplate.Triggers>
     <DataTrigger Binding="{Binding Content, RelativeSource={RelativeSource AncestorType={x:Type ContentControl}}}" Value="0">
         <Setter TargetName="WarningGrid" Property="Visibility" Value="Visible" />
     </DataTrigger>
  </DataTemplate.Triggers>