动态显示/显示wpf内容

动态显示/显示wpf内容,wpf,xaml,togglebutton,contentcontrol,Wpf,Xaml,Togglebutton,Contentcontrol,正如标题所说,我想动态隐藏或显示一些内容。我有两个按钮像单选按钮一样工作。每个按钮都必须显示特定的内容,但这两个内容不能同时可见(这是一种表单,我正在处理两个子类) 我在电视上看到了两件有趣的事情,我想把它们混合在一起。首先,使用带有切换按钮的ContentControl(http://stackoverflow.com/questions/7698560/how-to-bind-click-of-a-button-to-change-the-content-of-a-panel-grid-us

正如标题所说,我想动态隐藏或显示一些内容。我有两个按钮像单选按钮一样工作。每个按钮都必须显示特定的内容,但这两个内容不能同时可见(这是一种表单,我正在处理两个子类)

我在电视上看到了两件有趣的事情,我想把它们混合在一起。首先,使用带有切换按钮的ContentControl(http://stackoverflow.com/questions/7698560/how-to-bind-click-of-a-button-to-change-the-content-of-a-panel-grid-using-xaml). 其次,使用切换按钮作为单选按钮(http://stackoverflow.com/questions/2362641/how-to-get-a-group-of-toggle-buttons-to-act-like-radio-buttons-in-wpf 第二个答案(31)

所以我决定从这样的事情开始:

<ContentControl>
    <ContentControl.Template>
        <ControlTemplate>
            <WrapPanel HorizontalAlignment="Center">
                <TextBlock Text="{x:Static Internationalization:Resources.MAINOPTIONSWINDOW_STATUSLINKSUPERVISOR}"/>
                <RadioButton Style="{StaticResource {x:Type ToggleButton}}" Content="Alarm" GroupName="Trigger"/>
                <RadioButton Style="{StaticResource {x:Type ToggleButton}}" Content="Event" GroupName="Trigger"/>

            </WrapPanel>
        </ControlTemplate>
    </ContentControl.Template>
</ContentControl>
从视觉上看,我的按钮看起来像单选按钮,而不是切换按钮。 视频显示:

无法解析资源“{StaticResource{x:Type” ToggleButton}”(翻译自法语;)


无论如何,这段代码在ContentControl之外运行良好。

在控件模板中,通常不添加事件处理程序


覆盖OnApplyTemplate并在那里添加处理程序。

是的,首先,修复事件处理程序。不要将它们附加到模板上,可能会发生棘手的事情

其次,您可以修改ContentControl.Resources,并将其放在那里:

<Style BasedOn="{StaticResource {x:Type ToggleButton}}"
          TargetType="RadioButton"/>

并删除您自己的样式定义。您甚至可以将该级别再提高一级(ContentControl的父级)。还有一件事,您是在代码隐藏中完成的,但我可以向您保证,WPF的真正威力来自这样一个事实,即您不需要代码隐藏,至少对于这样的逻辑是这样的

还有你提供的链接,它使用触发器。现在这没什么大不了的,但你应该知道这不是正确的方法。如果您想在内容之间切换,请查看ContentControl内容、DataTemplates、DataTemplateSelector。这个想法是在记忆中只有一个视图,而不是隐藏东西


如果你用另一种方式做的话,这会迎头赶上。它最终将为启动和内存使用增加更多时间

我终于找到了另一种方法

<RadioButton Content="Alarm" GroupName="TriggerStateInfo" Style="{StaticResource {x:Type ToggleButton}}"
      Command="{x:Static StateInfoTemplateToAlarmCommand}"/>
<RadioButton Content="Event" GroupName="TriggerStateInfo" Style="{StaticResource {x:Type ToggleButton}}"
              Command="{x:Static StateInfoTemplateToEventCommand}"/>
<ContentControl Content="{Binding Path=Trigger, ElementName=Window}">
    <ContentControl.Resources>
        <DataTemplate DataType="{x:Type States:AlarmTrigger}">
            <ComboBox ItemsSource="{Binding Path=AlarmTriggersCollection}" />
        </DataTemplate>
        <DataTemplate DataType="{x:Type States:EventTrigger}">
            <ComboBox ItemsSource="{Binding Path=EventTriggersCollection}" />
        </DataTemplate>
    </ContentControl.Resources>
</ContentControl>

代码隐藏在更改触发器类型之后

<RadioButton Content="Alarm" GroupName="TriggerStateInfo" Style="{StaticResource {x:Type ToggleButton}}"
      Command="{x:Static StateInfoTemplateToAlarmCommand}"/>
<RadioButton Content="Event" GroupName="TriggerStateInfo" Style="{StaticResource {x:Type ToggleButton}}"
              Command="{x:Static StateInfoTemplateToEventCommand}"/>
<ContentControl Content="{Binding Path=Trigger, ElementName=Window}">
    <ContentControl.Resources>
        <DataTemplate DataType="{x:Type States:AlarmTrigger}">
            <ComboBox ItemsSource="{Binding Path=AlarmTriggersCollection}" />
        </DataTemplate>
        <DataTemplate DataType="{x:Type States:EventTrigger}">
            <ComboBox ItemsSource="{Binding Path=EventTriggersCollection}" />
        </DataTemplate>
    </ContentControl.Resources>
</ContentControl>