XAML将动态参数传递给方法 [英] XAML passing dynamic parameter to method

查看:74
本文介绍了XAML将动态参数传递给方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!

我正在开发WPF应用程序,其中包含窗口和文本块.
我有采用字符串参数的外部方法,并在XAML中即时通讯使用了该方法.

Hi all!

Im developing WPF application where is window and textblock within.
I have external method which takes string parameter and im using it in XAML.

<TextBlock Text="{MyMethod mystringparameter}" />                



现在,我试图将我的DependencyPropery"MyString"作为参数发送,但没有成功..



Now im trying to send my DependencyPropery "MyString" as parameter without success..

<TextBlock> Text="{MyMethod {Binding ElementName=myWindow, Path=MyString}} />



并在我的依赖项属性后面进行编码



and codebehind my Dependency Property

public string MyString
        {
            get { return (string)GetValue(MyStringProperty); }
            set { SetValue(MyStringProperty, value); }
        }

        // Using a DependencyProperty as the backing store for MyString.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MyStringProperty =
            DependencyProperty.Register("MyString", typeof(string), typeof(Window1), new UIPropertyMetadata("DefaultString"));



如何实现我的目标?

更新:

Littlebit更多信息. MyClass继承markupextension.它处理作为参数给定的资源密钥,并返回与其给定密钥相对应的值.因此,基本上我的目标是动态更改资源密钥.第一个textblock示例在静态设置key时起作用.

希望你有主意:)

干杯!



How can I achieve my goal?

UPDATE:

Littlebit more info. MyClass inherits markupextension. It handles resource keys given as parameter and returns value correponding its given key. So basically my goal is dynamically change resource keys. First textblock example works when key is statically set.

Hope you got idea :)

Cheers!

推荐答案


<TextBlock Text="{MyMethod mystringparameter}" /> 
<TextBlock Text="{MyMethod {Binding ElementName=myWindow, Path=MyString}} />


将无法工作,您不能在xaml中使用类似的方法.您可以执行以下三件事之一:

1)使用ObjectDataProvider [简单的WPF本地化 [


wont work, you can''t use a method like that in xaml. You can do one of the following three thing:

1)use ObjectDataProvider[^]
2) Create an attached property and do some reflection
3) Create your own markup extension

I suggest you do none of the above right now, and instead tell me what you''re trying to achieve I''m sure we can find another better way than using a method.



Ok I suspected that was what you were trying to do. I''ll not go over how you can do this here but refer you to an article that covers that subject:
Simple WPF Localization[^]
There is close to no explenation of how it''s done, but since you''ve already looked abit at MarkupExtension I''m sure you can look at the code and quickly figure out how it works. It also shows how you can avoid the need to register a namespace Like Wayne suggested in his answer.


您的TextBlock在写入时出现了synatx错误.这样写
Your TextBlock has synatx error as this is written <textblock> write this way
<textblock text="{Binding StData}"></textblock>



和后面的代码



and code behind

string StData;
        public string StData
        {
            get { return StData; }
            set
            {
                StData= value;
                
            }
        }


在使用MarkupExtension时,需要将MyString变量绑定为MarkupExtension的参数.像这样的东西:_

AS you are using a MarkupExtension, you need to then bind the MyString variable as the parameter for your MarkupExtension. Something like this:_

<textblock text="{ext:YourExtension YourParameter=" path="MyString}}"/"></textblock>



其中ext:是可以找到您的扩展名的名称空间.显然可以根据需要替换正确的变量名和扩展名.

希望对您有帮助



where ext: is the namespace where your extension can be found. Obviously substitute you correct variable and extension names as appropriate.

Hope this helps


这篇关于XAML将动态参数传递给方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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