绑定对对象的引用 [英] Bind reference to object
问题描述
大家好,
在C#/WPF应用程序中,我想知道是否可以将引用传递给对象的属性为
CommandParameter .
通常,我会执行类似的操作以将参数发送给命令:
In a C#/WPF application, I'd like to know if it's possible to pass the reference to an object's property as
CommandParameter.
Usually, I do something like this to send parameter to a command :
<Button Content="Send" Command="{x:static MyCommands.Send}" CommandParameter={Binding Path=Property1}"/>
是否可以将引用传递给 Property1 以从 ICommand ?
Is it possible to pass the reference to Property1 to initialize it from a ICommand ?
在C#中,键区设置很简单,但在WPF中...:(
In C#, it's easy with the keywoard out but in WPF ... :(
预先感谢您的建议.
推荐答案
我创建了以下DependencyObject:
I created the following DependencyObject :
public class SinglePropertyControl : Control
{
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register("Value", typeof(object), typeof(SinglePropertyControl));
public object Value
{
get { return GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); }
}
}
我将其添加到了Window的资源中:
I added it to the resources of my Window :
<Window.Resources>
<ct:SinglePropertyControl x:Key="Property" Value="{Binding Path=Property1}"/>
</Window.Resources>
我的按钮的绑定方式是这样的:
My button is bound like this:
<Button Content="Send" Command="{x:static MyCommands.Send}" CommandParameter={Binding Source="{StaticResource Property}" Path=Property1}"/>
在我的ICommand中,我将参数作为SinglePropertyControl ...
And in my ICommand, I work on the parameter as SinglePropertyControl ...
如果您有更好的主意...
If you have a better idea ...
这篇关于绑定对对象的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!