我有一个自定义的 SplitButton 实现,其中包含一个 ComboBox,其中有多个绑定到命令的 ComboBoxItem。我可以很好地绑定到命令的 Name 和 Text 属性,但无法将 ComboBoxItem 的IsEnabled属性绑定到 Command 的CanExecute方法的结果,因为它是一个方法。是否有一些我不知道的用于绑定到方法的语法,或者是否有一些技巧可以帮助我绑定到 CanExecute。
顺便说一句,我考虑过使用自定义 ValueConverter,除了我意识到重新评估 CanExecute 时我可能不会收到任何更新,因为它不是属性,而且我的命令不是业务对象。在我看来,此时我可能必须为命令创建一个 ViewModel,以便仅在我的自定义 SplitButton 控件中使用,但这对我来说似乎有点过分了。
我有一个Button和它的风格:
<Button Name="MyBtn" Style="{StaticResource ButtonEnabledStyle}"
IsEnabled="False" Opacity="1" />
<Style x:Key="ButtonEnabledStyle" TargetType="Button">
<Style.Triggers>
<Trigger Property="IsEnabled" Value="True" >
<Setter Property="Opacity" Value="0.1" />
</Trigger>
</Style.Triggers>
</Style>
Run Code Online (Sandbox Code Playgroud)
但是当我启用Button(MyBtn.IsEnabled = true)时,它不会改变它的不透明度.为什么?我怎么解决这个问题?谢谢.
我在这里找到的解决方案使用的.enabled是旧的,而不是.isEnabled.
因此,如果满足(或不满足)某个条件,我正在尝试仅禁用按钮的功能/可点击性。所以在这一切之前,如果不满足条件,我将它们全部禁用,然后如果在(动态)之后满足它,那么理论上应该启用。话虽如此,当我从.isEnabled = false.
我知道满足条件是因为我有打印语句和其他测试(比如标签从超级视图中删除但.isEnabled = false按钮不起作用。有人遇到过上述问题或有任何解决方案吗?
代码如下:
override func viewDidLoad()
{
super.viewDidLoad()
trumpMoneyDefault.setValue(50, forKey: "TrumpMoney")
print("UnoViewController")
//make all the buttons that shouldn't be clickable unlcickable
locklvl2.isEnabled = false
locklvl3.isEnabled = false
trumplvl2.isEnabled = false
trumplvl3.isEnabled = false
lvl2.isEnabled = false
lvl3.isEnabled = false
//make level2/3 unclickable by defeault
//lvl2.isEnabled = false
//lvl3.isEnabled = false
//update trumpmoney label depending on if they have enough cash
//also here check if they have already …Run Code Online (Sandbox Code Playgroud) 我在我的窗口中定义了依赖属性,如下所示:
public static readonly DependencyProperty IsGenericUserProperty = DependencyProperty.Register("IsGenericUser", typeof (bool), typeof (MainWindow));
public bool IsGenericUser
{
get { return (bool) GetValue(IsGenericUserProperty); }
set { SetValue(IsGenericUserProperty, value); }
}
Run Code Online (Sandbox Code Playgroud)
在我的窗口的构造函数中,我设置了包含按钮的容器的数据上下文:
QuickListButtonsStackPanel.DataContext = this;
Run Code Online (Sandbox Code Playgroud)
我将依赖项属性绑定到按钮的IsEnabled属性:
<Button IsEnabled="{Binding IsGenericUser}" .../>
Run Code Online (Sandbox Code Playgroud)
在启动IsGenericUser时为true,因此启用该按钮.当我将IsGenericUser设置为false时,该按钮被禁用.但是,如果我再次使IsGenericUser为true,则按钮没有任何反应,它仍保持禁用状态.我究竟做错了什么?
谢谢!
编辑:这是我使用按钮的样式.此样式导致问题(如果按钮没有自定义样式,它可以正常工作):
<Style x:Key="BlackButtonStyle" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<ControlTemplate.Resources>
<Storyboard x:Key="MouseOverActivating">
<ColorAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="rectangle" Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)">
<SplineColorKeyFrame KeyTime="00:00:00" Value="#FF2F2F2F"/>
<SplineColorKeyFrame KeyTime="00:00:00.1270000" Value="#FF2391FF"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Key="MouseOverDeactivating">
<ColorAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="rectangle">
<SplineColorKeyFrame KeyTime="00:00:00" Value="#FF2391FF"/>
<SplineColorKeyFrame KeyTime="00:00:00.2200000" Value="#FF2F2F2F"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
<Storyboard …Run Code Online (Sandbox Code Playgroud) 我试图设置一个按钮,所以只有当鼠标移过它时,按钮才会将其视觉属性更改为启用按钮.
这是我的代码:
<Button Foreground="Black" Content="OK" Margin="186,100,170,172" >
<Button.Style>
<Style TargetType="Button">
<Setter Property="IsEnabled" Value="False" />
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="IsEnabled" Value="True" />
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
Run Code Online (Sandbox Code Playgroud)
在此先感谢您的帮助.
当IsEnabled为false时,我希望图像的不透明度为.50.我一直在看多个例子,但我仍然无法掌握如何使它工作.
这是我的自定义控件的完整XAML.任何帮助将深表感谢.
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="test.StopButtonControl"
x:Name="UserControl"
d:DesignWidth="85" d:DesignHeight="85">
<Grid x:Name="LayoutRoot">
<Image x:Name="StopButtonUI" Source="Images/stop.png" Stretch="Fill" MouseUp="StopButtonClick"/>
</Grid>
</UserControl>
Run Code Online (Sandbox Code Playgroud) 我是WPF的新手,过去我使用的是Windows Forms.我在这里有一个问题,我喜欢有人向我解释.以下是一个非常简单的例子.
我有一个XAML页面,我有一个复选框,一个按钮和一个文本字段.默认情况下会选中该复选框.
取消选中该复选框后,我想启用按钮和文本字段,例如
private void UseDefaultFoldersCB_Checked(object sender, RoutedEventArgs e)
{
//MessageBox.Show("");
if (StartDirLocationTB.IsEnabled == false)
{
StartDirLocationTB.IsEnabled = true;
}
if (SelectStartLocationBtn.IsEnabled == false)
{
SelectStartLocationBtn.IsEnabled = true;
}
}
Run Code Online (Sandbox Code Playgroud)
XAML:
<CheckBox Content="Use Default Folders" IsChecked="True" Height="16" HorizontalAlignment="Left" Margin="10,14,0,0" Name="UseDefaultFoldersCB" VerticalAlignment="Top" Checked="UseDefaultFoldersCB_Checked" />
<TextBox Height="23" IsEnabled="False" HorizontalAlignment="Left" Margin="9,38,0,0" Name="StartDirLocationTB" VerticalAlignment="Top" Width="403" Background="WhiteSmoke" />
<Button Content="Select Start Folder" IsEnabled="False" Height="23" HorizontalAlignment="Right" Margin="0,38,6,0" Name="SelectStartLocationBtn" VerticalAlignment="Top" Width="139" />
Run Code Online (Sandbox Code Playgroud)
堆栈跟踪:
用户代码未处理System.NullReferenceException
消息=未将对象引用设置为对象的实例.
Source = TestProject StackTrace:位于C:\ Users\jc\Desktop\Test\TestProject\MainWindow.xaml.cs中的TestProject.MainWindow.UseDefaultFoldersCB_Checked(Object sender,RoutedEventArgs e):System.Windows.EventRoute.InvokeHandlersImpl中的第611行(对象在System.Windows.DependencyObject.OnPropertyChanged的System.Windows.Controls.Primitives.ToggleButton.OnIsCheckedChanged(DependencyObject d,DependencyPropertyChangedEventArgs e)上的System.Windows.UIElement.RaiseEventImpl(DependencyObject sender,RoutedEventArgs args)中的source,RoutedEventArgs …
我有一个MVVM应用程序包含多个视图,其中包含一些IsReadOnly基于用户权限,查看/编辑模式和对象状态的复杂规则.
我想设置IsReadOnly和/或IsEnabled用于控制的整个组的属性在同一容器(GroupBox/ StackPanel/ Grid/ UserControl/等).此属性的值将在ViewModel中定义.
我有3-6不同的SomeGroupIsReadOnly每用户控件的属性(也像大量输入控件TextBox,RadioButtons,ComboBoxes和一些DataGrids),我正在寻找一个通用的,MVVM-友好的解决方案,这将让我重新使用绑定在每个容器而不是分别为每个单独的控件指定它们.
如何使用XAML在Panel或GroupBox等容器内的所有控件上设置IsReadOnly/IsEnabled?
WPF似乎不支持这种开箱即用的...
编辑
我忘了提到为容器设置IsEnabled会禁用TextBoxes的一个重要功能 - 能够复制它们的内容.我需要他们处于IsReadOnly=true状态.如果有一个工作方法,那么我的问题将得到解决.
我们如何能够让squid缓存Web内容(让我们从firefox说)用于SSL连接,我的意思是https URL?
我想知道我是否可以将我的Button isEnabled绑定到我的StackPanel Children(有孩子).如果stackpanel有子项,那么我的按钮被启用,没有孩子我的按钮被禁用.目前我只是在代码中处理这个,但我开始想知道这是否是我可以绑定的东西.谢谢你的任何想法......