C# 在XAML中,如何在不使用数据绑定的情况下引用非静态属性?
我和一个转换器一起工作-C# 在XAML中,如何在不使用数据绑定的情况下引用非静态属性?,c#,wpf,xaml,C#,Wpf,Xaml,我和一个转换器一起工作- public class EnumSwitchConverter : ConverterBase { private object _On = null, _Off = null; /// <summary> /// Get or Set object returned when parameter is present in value. /// </summary>
public class EnumSwitchConverter : ConverterBase {
private object
_On = null,
_Off = null;
/// <summary>
/// Get or Set object returned when parameter is present in value.
/// </summary>
public object On {
get { return this._On; }
set { this._On = value; }
}
/// <summary>
/// Get or Set object returned when parameter is not present in value.
/// </summary>
public object Off {
get { return this._Off; }
set { this._Off = value; }
}
public override object Convert(
object value, Type targetType, object parameter, CultureInfo culture ) {
return ( ( Enum )value ).HasFlag( ( Enum )parameter )
? this.On : this.Off;
}
}
我有两个属性,它们公开了我想要打开和关闭的命令
public partial class App : Application {
/*Stuff*/
public ICommand onCommand{ get{ return this._onCommand; } }
public ICommand offCommand{ get{ return this._offCommand; } }
}
我不想做以下事情-
On={Binding OnCommand, Source={x:Static Application.Current}
首先,OnCommand
永远不会改变。其次,上的不是依赖项属性,所以我无论如何也不能这样做。我只想传递OnCommand
作为对转换器的属性的的引用
如何在转换器中引用命令属性?我可以立即想到两种方法(加上@mm8指出的第三种方法):
将命令设置为静态
public partial class App : Application {
public static ICommand OnCommand { get{ return _onCommand; } }
public static ICommand OffCommand { get{ return _offCommand; } }
}
XAML
XAML
您需要了解您在这里所做的工作:标记扩展只在解析XAML时计算一次。它是一个标记扩展GetProp
将为您提供该属性当时的值。如果它是一个没有setter的命令属性,在构造函数中初始化,或者在第一次有人调用getter时初始化,那么这也没关系。这就是我们的情况。但很多时候,情况并非如此
mm8建议的另一个选项是从DependencyObject
而不是从MarkupExtension派生valueconverter。这意味着您必须在资源中创建它的实例,但这并不是世界末日。然后您可以为它提供可绑定的依赖属性
如果您真的厌倦了StaticResource
value converter,那么可以编写一个MarkupExtension
,返回依赖对象/值转换器类的实例。MarkupExtension
可以镜像其属性并将值转发给它。但我看不到任何投资回报率在走这么远
我可以立即想到两种方法(加上@mm8指出的第三种方法):
将命令设置为静态
public partial class App : Application {
public static ICommand OnCommand { get{ return _onCommand; } }
public static ICommand OffCommand { get{ return _offCommand; } }
}
XAML
XAML
您需要了解您在这里所做的工作:标记扩展只在解析XAML时计算一次。它是一个标记扩展GetProp
将为您提供该属性当时的值。如果它是一个没有setter的命令属性,在构造函数中初始化,或者在第一次有人调用getter时初始化,那么这也没关系。这就是我们的情况。但很多时候,情况并非如此
mm8建议的另一个选项是从DependencyObject
而不是从MarkupExtension派生valueconverter。这意味着您必须在资源中创建它的实例,但这并不是世界末日。然后您可以为它提供可绑定的依赖属性
如果您真的厌倦了StaticResource
value converter,那么可以编写一个MarkupExtension
,返回依赖对象/值转换器类的实例。MarkupExtension
可以镜像其属性并将值转发给它。但我看不到任何投资回报率在走这么远
给App
一个静态Current
属性,该属性返回(App)Application.Current
并使用{x:static App.Current.onCommand}
等。@EdPlunkett{x:static App.Instance.onCommand}
-“嵌套类型不受支持:App.Instance”。好的,刚刚测试过它。它必须是{x:Static local:App.Current.onCommand}
,但是它没有工作,因为(我认为)整个属性路径必须是静态的。但是,我尝试将OnCommand
设置为App
的静态属性,结果成功:{x:static local:App.OnCommand}
。您还可以编写另一个MarkupExtension
,它使用一个枚举表示需要哪个命令(或一个字符串表示属性名称),并从(App)获取该属性Application.Current
,带有if/else、开关或反射。(再想一想,这只能在评估MarkupExtension时实际给出属性的值,这可能有用,也可能无用)。@EdPlunkett所以引用属性的唯一方法是使用绑定,或者如果属性是静态的?不可能是这样的。必须有某种方法来引用非静态属性而不使用绑定…给App
一个静态Current
属性,该属性返回(App)Application.Current
,并使用{x:static App.Current.onCommand}
等@EdPlunkett{x:static App.Instance.onCommand}
-“嵌套类型不受支持:App.Instance。”好的,只是测试了它。它必须是{x:Static local:App.Current.onCommand}
,但它不起作用,因为(我认为)整个属性路径必须是静态的。然而,我尝试将onCommand
设置为App
的静态属性,这起到了作用:{x:Static local:App.OnCommand}
。您还可以编写另一个标记扩展名
,该扩展名使用一个枚举表示所需的命令(或属性名称的字符串),并从(App)应用程序.Current
中获取,其中包含if/else、开关或反射。(再想一想,这只能现实地给出加价延期评估时财产的价值,这可能有用,也可能无用).@EdPlunkett所以引用属性的唯一方法是如果我使用绑定,或者如果属性是静态的?这不可能。必须有某种方法引用非静态属性而不使用绑定…我真的很喜欢#2,现在我可以在代码中看到它了…我只希望有某种方法引用属性而不必执行绑定像这样绑定或跳过一千个不同的环,但我可以接受这个选项。mm8建议的另一个选项是从DependencyObject而不是MarkupExtension派生您的valueconverter。这意味着您必须在参考资料中创建它的一个实例,但这并不是结束
<Button
Command="{Binding
PropertySource.SomeEnumProperty,
Source={x:Static Application.Current},
Converter={myConverters:EnumConverter
On={x:Static local:App.OnCommand},
Off={x:Static local:App.OffCommand}}
ConverterParameter={x:Static myEnum:enumValue}}"
/>
public class GetInstanceProp : MarkupExtension
{
public GetInstanceProp(String propname, Object source)
{
_source = source;
_propName = propname;
}
private Object _source;
private String _propName;
public override object ProvideValue(IServiceProvider serviceProvider)
{
var propInfo = _source.GetType().GetProperty(_propName);
return propInfo.GetValue(_source);
}
}
<Button
Command="{Binding
PropertySource.SomeEnumProperty,
Source={x:Static Application.Current},
Converter={myConverters:EnumConverter
On={local:GetInstanceProp OnCommand, {x:Static local:App.Current}},
Off={local:GetInstanceProp OffCommand, {x:Static local:App.Current}}
ConverterParameter={x:Static myEnum:enumValue}}"
/>