如何绑定单击按钮以使用XAML更改面板(Grid)的内容
问题描述:
我正在创建WPF应用程序的UI,并且在处理该软件功能的实现时,我没有太多经验创建用户界面。如何绑定单击按钮以使用XAML更改面板(Grid)的内容
现在我需要一种方法来更改具有包含内容的网格的属性面板的内容。我创建了多个面板,隐藏除一个以外的所有面板,现在当用户单击顶部功能区中的按钮时(或者它可能是布局中其他任何按钮),现在我想切换。
这是很容易做的代码,但我想没有任何代码,只使用XAML。怎么做?
另外如何将相似的行为绑定到UI上的其他项目?
答
我认为您选择的仅适用于XAML的解决方案将取决于您的具体要求。在下面的示例中,我假设仅使用XAML意味着您正在寻找不涉及绑定到ViewModel中的属性的解决方案。
方法#1:
如果你决定使用一个单一的ToggleButton
显示和隐藏面板,则可以做到这一点很容易使用Triggers
:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<ContentControl>
<ContentControl.Template>
<ControlTemplate>
<StackPanel>
<Grid x:Name="myGrid" Background="Beige" Height="100">
<TextBlock Text="Content Placeholder" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="5"/>
</Grid>
<ToggleButton x:Name="toggleButton" Content="Show\Hide Panel" IsChecked="True"/>
</StackPanel>
<ControlTemplate.Triggers>
<Trigger SourceName="toggleButton" Property="IsChecked" Value="True">
<Setter TargetName="myGrid" Property="Visibility" Value="Visible" />
</Trigger>
<Trigger SourceName="toggleButton" Property="IsChecked" Value="False">
<Setter TargetName="myGrid" Property="Visibility" Value="Hidden" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</ContentControl.Template>
</ContentControl>
</Window>
方法#2 :
如果您需要两个按钮(一个用于显示面板,一个用于隐藏面板),那么也许您可以使用改为EventTrigger
。由于EventTrigger
不使用Setter
's,所以此解决方案更加严格,但其操作必须是Storyboard
。为了模拟像Visibility
一个属性,你可以使用ObjectAnimationUsingKeyFrames
的设置在Storyboard
:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<Grid x:Name="myGrid" Background="Beige" Height="100">
<TextBlock Text="Content Placeholder" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="5"/>
</Grid>
<Button x:Name="showPanelButton" Content="Show Panel" />
<Button x:Name="hidePanelButton" Content="Hide Panel" />
<StackPanel.Triggers>
<EventTrigger RoutedEvent="UIElement.PreviewMouseLeftButtonUp" SourceName="showPanelButton">
<BeginStoryboard>
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="myGrid" Storyboard.TargetProperty="(UIElement.Visibility)">
<DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="UIElement.PreviewMouseLeftButtonUp" SourceName="hidePanelButton">
<BeginStoryboard>
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="myGrid" Storyboard.TargetProperty="(UIElement.Visibility)">
<DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Hidden}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</StackPanel.Triggers>
</StackPanel>
</Window>
希望这有助于!
由于某种原因,程序挂起并且调试器弹出! – SpeedBirdNine
我使用.NET 4.0项目创建了这个示例。在.NET 3.5项目中,您将收到一个错误,指出WPF不知道如何将字符串(“PreviewMouseLeftButtonUp”)转换为RoutedEvent。我已经通过将'RoutedEvent'明确指定为UIElement.PreviewMouseLeftButtonUp来编辑上述示例,现在应该可以工作。 –
使用.NET 4.0,请参阅此问题:http://stackoverflow.com/questions/7702176/wpf-trigger-not-working-what-am-i-doing-wrong/7702494#7702494 – SpeedBirdNine