Click here to Skip to main content
15,885,944 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

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


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

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


and codebehind my Dependency Property

C#
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"));


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!
Posted
Updated 7-Sep-11 21:18pm
v2
Comments
Wayne Gaylard 8-Sep-11 2:59am    
You need to explain your goals more clearly. Are the TextBlocks you mentioned the same TextBlock ? What does the method that you mentioned return, and what is your intention binding the method to the Text ? If you just want to bind the TextBlock to the Property MyString, you just need to do <textblock text="{Binding" path="MyString}">, although I am probably missing your point here.
paleGegg0 8-Sep-11 3:21am    
updated

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

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.
 
Share this answer
 
v3
Comments
paleGegg0 8-Sep-11 3:22am    
question updated
Simon Bang Terkildsen 8-Sep-11 4:13am    
answer updated
Your TextBlock has synatx error as this is written <textblock> write this way
C#
<textblock text="{Binding StData}"></textblock>


and code behind

C#
string StData;
        public string StData
        {
            get { return StData; }
            set
            {
                StData= value;
                
            }
        }
 
Share this answer
 
Comments
Simon Bang Terkildsen 8-Sep-11 2:58am    
The entire problem isn't that the binding isn't written correctly.
The problem is the OP believes he can "bind" to a method and in that attempt writes a weird binding.
Anuja Pawar Indore 8-Sep-11 3:23am    
I suggest refer the link

http://blogs.msdn.com/b/mikehillberg/archive/2008/05/29/trying-out-binding-stringformat.aspx
Simon Bang Terkildsen 8-Sep-11 4:14am    
Where did string formatting come into the picture?
AS you are using a MarkupExtension, you need to then bind the MyString variable as the parameter for your MarkupExtension. Something like this:_

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


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

Hope this helps
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900