使用WPF按钮切换布尔值 [英] Toggle a boolean value with a WPF-button

查看:64
本文介绍了使用WPF按钮切换布尔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个愚蠢的问题,但无论如何,我需要帮助解决这个问题!


我的应用程序中有几个控件。我想有一个按钮(普通的WPF按钮)来控制其他几个WPF控件的IsEnabled属性。也就是说,我希望我的按钮在我的ViewModel中切换一个布尔值(我使用MVVM)
并让其他控件绑定到该属性。


示例:

默认情况下,布尔值应设置为False,这意味着其他控件IsEnable-property也为False

如果用户按下按钮,布尔值应设置为true和其他控件应该得到IsEnabled = True

如果用户再次按下按钮,则布尔值和控件IsEnabled-property应设置为False


我的想法是,我可以通过使用命令(或触发器)来解决这个问题,然后将ValueConverter用于其他控件IsEnabled-property,但我没有这个工作。


<有没有人知道如何让按钮影响ViewModel中的布尔值以启用此行为?

解决方案

您可以使用一对触发器在xaml中完全处理此问题每个播放故事板的切换按钮上的rs。看看:


 
< 窗口
xmlns = " http: //schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x = " http://schemas.microsoft.com/winfx/2006/xaml "
xmlns:Microsoft_Windows_Themes = " clr-namespace:Microsoft.Windows.Themes; assembly = PresentationFramework.Aero "
的xmlns:d <温泉n style ="color:Blue;"> = " http://schemas.microsoft .com / expression / blend / 2008 " xmlns:mc = " http://schemas.openxmlformats.org / markup-compatibility / 2006 " mc:可忽略 = " d "
x:Class = < span style ="color:Black;">" WpfApplication1.Window1 "
x:名称 = " 窗口 "
标题 = " Window1 "
宽度 = " 200 "
高度 = " 300 " >
< Window.Resources >
< 故事板 x:键 = &quo t; SB_DisableButtons " >
< BooleanAnimationUsingKeyFrames Storyboard.TargetProperty = " (UIElement.IsEnabled) " Storyboard.TargetName = " 按钮 < span style ="color:Black;">" >
< DiscreteBooleanKeyFrame KeyTime = " 0 & " = " ; False " / >
< / BooleanAnimationUsingKeyFrames >
< BooleanAnimationUsingKeyFrames Storyboard.TargetProperty = " (UIElement.IsEnabled) " Storyboard .TargetName = " button1 " >
< DiscreteBooleanKeyFrame KeyTime = " 0 " = " False " />
< / BooleanAnimationUsingKeyFrames >
< BooleanAnimationUsingKeyFrames Storyboard.TargetProperty = " (UIElement.IsEnabled) " Storyboard.TargetName = " button2 " >
< DiscreteBooleanKeyFrame KeyTime = " 0 < span style ="color:Black;">" Value = " False " />
< / BooleanAnimationUsingKeyFrames >
< / 故事板 >
< 故事板 x:Key = " SB_EnableButtons " >
< BooleanAnimationUsingKeyFrames Storyboard.TargetProperty = " (UIElement.IsEnabled) " Storyboard.TargetName = " 按钮 " >
< DiscreteBooleanKeyFrame KeyTime = " 0 " = " " />
< / BooleanAnimationUsingKeyFrames >
< BooleanAnimationUsingKeyFrames Storyboard .TargetProperty = " (UIElement.IsEnabled) " Storyboard.TargetName = " button1 " >
< DiscreteBooleanKeyFrame KeyTime = " 0 " ; = " True " /> ;
< / BooleanAnimationUsingKeyFrames >
< BooleanAnimationUsingKeyFrames < span style ="color:Red ;"> Storyboard.TargetProperty
= " (UIElement.IsEnabled) " Storyboard.TargetName = " button2 " >
< DiscreteBooleanKeyFrame KeyTime = " 0 " = " True " />
< / BooleanAnimationUsingKeyFrames >
< span style ="color:Blue;">< / 故事板 >
< / Window.Resources >
< Window.Triggers < span style ="color:Blue;">>
< EventTrigger RoutedEvent = " ToggleButton.Checked " SourceName = " toggleButton " >
< BeginStoryboard x:名称 = < span style ="color:Black;">" SB_EnableButtons_BeginStoryboard " < span style ="color:Red;"> Storyboard = " {StaticResource SB_EnableButtons} " />
< / EventTrigger >
< EventTrigger RoutedEvent = " ToggleButton.Unchecked " SourceName = " toggleButton " >
< BeginStoryboard x:名称 = " SB_DisableButtons_BeginStoryboard " Storyboard = " {StaticResource SB_DisableButtons} " />
< / EventTrigger >
< / Window.Triggers >
< 网格 >
< StackPanel Horizo​​ntalAlignment = " " 方向 = " 垂直 " VerticalAlignment = " Top " 宽度 = " 78.257 " >
< ; ToggleButton x:名称 = " toggleButton " 内容 = " ; ToggleButton " d :LayoutOverrides = " 宽度 " />
< Butto n x:名称 = " 按钮 " 内容 = " 按钮 " IsEnabled = " False " />
< 按钮 x:名称 = " button1 " 内容 = " 按钮 " IsEnabled = " 错误 " />
< 按钮 x:名称 < span style ="color:Blue;"> = " button2 " 内容 = " 按钮 " IsEnabled = " False " />
< / StackPanel >
< / 网格 >
< / 窗口 >


Hi, this is probably a stupid question but anyway, I need help with this issue!

I have several controls in my application. I would like to have one button (ordinary WPF-button) that controls the IsEnabled-property of several other WPF-controls. That is, I would like my button to toggle a boolean value in my ViewModel (I'm using MVVM) and let the other controls bind to that property.

Example:
By default the boolean value should be set to False which means that the other controls IsEnable-property is also False
If a user press the button the boolean should be set to true and the other controls should get IsEnabled = True
If a user press the button again both the boolean value and the controls IsEnabled-property should be set to False

My thought was that I could solve this problem by using commands (or triggers) and then use a ValueConverter for the other controls IsEnabled-property, but I haven't got this to work.

Does anyone have an idea on how I can get the button to affect the boolean in the ViewModel to enable this behaviour?

解决方案

You can handle this entirely in the xaml with a pair of triggers on the toggle button that each play a storyboard. Take a look:

<Window
	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
	xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"
	x:Class="WpfApplication1.Window1"
	x:Name="Window"
	Title="Window1" 
	Width="200" 
	Height="300">
	<Window.Resources>
		<Storyboard x:Key="SB_DisableButtons">
			<BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.IsEnabled)" Storyboard.TargetName="button">
				<DiscreteBooleanKeyFrame KeyTime="0" Value="False"/>
			</BooleanAnimationUsingKeyFrames>
			<BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.IsEnabled)" Storyboard.TargetName="button1">
				<DiscreteBooleanKeyFrame KeyTime="0" Value="False"/>
			</BooleanAnimationUsingKeyFrames>
			<BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.IsEnabled)" Storyboard.TargetName="button2">
				<DiscreteBooleanKeyFrame KeyTime="0" Value="False"/>
			</BooleanAnimationUsingKeyFrames>
		</Storyboard>
		<Storyboard x:Key="SB_EnableButtons">
			<BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.IsEnabled)" Storyboard.TargetName="button">
				<DiscreteBooleanKeyFrame KeyTime="0" Value="True"/>
			</BooleanAnimationUsingKeyFrames>
			<BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.IsEnabled)" Storyboard.TargetName="button1">
				<DiscreteBooleanKeyFrame KeyTime="0" Value="True"/>
			</BooleanAnimationUsingKeyFrames>
			<BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.IsEnabled)" Storyboard.TargetName="button2">
				<DiscreteBooleanKeyFrame KeyTime="0" Value="True"/>
			</BooleanAnimationUsingKeyFrames>
		</Storyboard>
	</Window.Resources>
	<Window.Triggers>
		<EventTrigger RoutedEvent="ToggleButton.Checked" SourceName="toggleButton">
			<BeginStoryboard x:Name="SB_EnableButtons_BeginStoryboard" Storyboard="{StaticResource SB_EnableButtons}"/>
		</EventTrigger>
		<EventTrigger RoutedEvent="ToggleButton.Unchecked" SourceName="toggleButton">
			<BeginStoryboard x:Name="SB_DisableButtons_BeginStoryboard" Storyboard="{StaticResource SB_DisableButtons}"/>
		</EventTrigger>
	</Window.Triggers>
<Grid>
	<StackPanel HorizontalAlignment="Left" Orientation="Vertical" VerticalAlignment="Top" Width="78.257">
		<ToggleButton x:Name="toggleButton" Content="ToggleButton" d:LayoutOverrides="Width"/>
		<Button x:Name="button" Content="Button" IsEnabled="False"/>
		<Button x:Name="button1" Content="Button" IsEnabled="False"/>
		<Button x:Name="button2" Content="Button" IsEnabled="False"/>
	</StackPanel>
</Grid>
</Window>


这篇关于使用WPF按钮切换布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆