【问题标题】:Dynamically show/display wpf content动态显示/显示 wpf 内容
【发布时间】:2012-08-24 08:02:06
【问题描述】:

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

我在 SO 上看到了两件有趣的事情,我想把它们混合起来。首先,使用带有 ToggleButton 的 ContentControl (http://stackoverflow.com/questions/7698560/how-to-bind-click-of-a-button-to-change-the-content-of-a-panel-网格使用 xaml)。其次,使用 ToggleButtons 作为 RadioButtons (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>

但 Visual Studio 下划线

{StaticResource {x:Type ToggleButton}}

在视觉上,我的按钮看起来像单选按钮而不是切换按钮。 视觉说:

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

无论如何,这段代码在 ContentControl 之外可以正常工作。

【问题讨论】:

    标签: wpf xaml togglebutton contentcontrol


    【解决方案1】:

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

    其次,你可以修改你的 ContentControl.Resources,然后把它放在那里:

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

    并删除您自己的样式定义。您甚至可以再提高一层(ContentControl 的父级)。还有一件事,您在代码隐藏中执行此操作,但我可以向您保证,WPF 的真正强大之处在于您不需要代码隐藏,至少对于这样的逻辑。

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

    如果你反其道而行之,那就可以赶上。它最终会增加启动时间和内存使用量。

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        【解决方案3】:

        我终于找到了另一种方式

        <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>
        

        在更改触发器类型背后的代码

        【讨论】:

          猜你喜欢
          • 2016-07-18
          • 1970-01-01
          • 1970-01-01
          • 2018-01-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-11-04
          • 1970-01-01
          相关资源
          最近更新 更多