Rus*_*uss 356 wpf styles .net-3.5
我拥有的是具有IsReadOnly属性的对象.如果此属性为true,我想将IsEnabledButton(例如)上的属性设置为false.
我想相信我可以轻松地做到这一点,IsEnabled="{Binding Path=!IsReadOnly}"但不能与WPF一起使用.
我不得不经历所有的风格设置吗?对于像将一个bool设置为另一个bool的反向那样简单的事情,似乎太过冗长.
<Button.Style>
<Style TargetType="{x:Type Button}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsReadOnly}" Value="True">
<Setter Property="IsEnabled" Value="False" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=IsReadOnly}" Value="False">
<Setter Property="IsEnabled" Value="True" />
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
Run Code Online (Sandbox Code Playgroud)
Chr*_*col 462
您可以使用ValueConverter为您反转bool属性.
XAML:
IsEnabled="{Binding Path=IsReadOnly, Converter={StaticResource InverseBooleanConverter}}"
Run Code Online (Sandbox Code Playgroud)
转换器:
[ValueConversion(typeof(bool), typeof(bool))]
public class InverseBooleanConverter: IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
if (targetType != typeof(bool))
throw new InvalidOperationException("The target must be a boolean");
return !(bool)value;
}
public object ConvertBack(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
#endregion
}
Run Code Online (Sandbox Code Playgroud)
Pau*_*der 97
你考虑过IsNotReadOnly房产吗?如果绑定的对象是MVVM域中的ViewModel,则附加属性非常有意义.如果它是直接实体模型,您可以考虑组合并向表单呈现实体的专用ViewModel.
Ale*_*141 66
使用标准装订,您需要使用看起来有风的转换器.因此,我建议您查看我的项目CalcBinding,它是专门为解决此问题而开发的.使用高级绑定,您可以直接在xaml中编写具有许多源属性的表达式.说,你可以这样写:
<Button IsEnabled="{c:Binding Path=!IsReadOnly}" />
Run Code Online (Sandbox Code Playgroud)
要么
<Button Content="{c:Binding ElementName=grid, Path=ActualWidth+Height}"/>
Run Code Online (Sandbox Code Playgroud)
要么
<Label Content="{c:Binding A+B+C }" />
Run Code Online (Sandbox Code Playgroud)
要么
<Button Visibility="{c:Binding IsChecked, FalseToVisibility=Hidden}" />
Run Code Online (Sandbox Code Playgroud)
其中A,B,C,IsChecked - viewModel的属性,它将正常工作
祝好运!
Nox*_*xys 20
我建议使用https://quickconverter.codeplex.com/
然后,反转布尔值就像这样简单:
<Button IsEnabled="{qc:Binding '!$P', P={Binding IsReadOnly}}" />
这加快了编写转换器通常所需的时间.
小智 13
我希望我的XAML保持尽可能优雅,所以我创建了一个类来包装驻留在我的一个共享库中的bool,隐式运算符允许该类在代码隐藏中无缝地用作bool
public class InvertableBool
{
private bool value = false;
public bool Value { get { return value; } }
public bool Invert { get { return !value; } }
public InvertableBool(bool b)
{
value = b;
}
public static implicit operator InvertableBool(bool b)
{
return new InvertableBool(b);
}
public static implicit operator bool(InvertableBool b)
{
return b.value;
}
}
Run Code Online (Sandbox Code Playgroud)
项目所需的唯一更改是使您想要反转的属性返回而不是bool
public InvertableBool IsActive
{
get
{
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
并且在XAML后缀中绑定了Value或Invert
IsEnabled="{Binding IsActive.Value}"
IsEnabled="{Binding IsActive.Invert}"
Run Code Online (Sandbox Code Playgroud)
Ωme*_*Man 11
.Net核心解决方案
处理 null 情况,不抛出异常,但true如果没有值则返回;否则获取输入的布尔值并将其反转。
public class BooleanToReverseConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
=> !(bool?) value ?? true;
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
=> !(value as bool?);
}
Run Code Online (Sandbox Code Playgroud)
沙姆尔
IsEnabled="{Binding IsSuccess Converter={StaticResource BooleanToReverseConverter}}"
Run Code Online (Sandbox Code Playgroud)
App.Xaml我喜欢将所有转换器静态数据放在 app.xaml 文件中,这样我就不必在项目的整个窗口/页面/控件中重新声明它们。
<Application.Resources>
<converters:BooleanToReverseConverter x:Key="BooleanToReverseConverter"/>
<local:FauxVM x:Key="VM" />
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)
需要明确的 converters:是实际类实现的命名空间 ( xmlns:converters="clr-namespace:ProvingGround.Converters")。
这个也适用于可空的布尔.
[ValueConversion(typeof(bool?), typeof(bool))]
public class InverseBooleanConverter : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (targetType != typeof(bool?))
{
throw new InvalidOperationException("The target must be a nullable boolean");
}
bool? b = (bool?)value;
return b.HasValue && !b.Value;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return !(value as bool?);
}
#endregion
}
Run Code Online (Sandbox Code Playgroud)
小智 6
在您的视图模型中再添加一个属性,它将返回反向值。并将其绑定到按钮。喜欢;
在视图模型中:
public bool IsNotReadOnly{get{return !IsReadOnly;}}
Run Code Online (Sandbox Code Playgroud)
在 xaml 中:
IsEnabled="{Binding IsNotReadOnly"}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
181683 次 |
| 最近记录: |