Get DynamicResource Binding in WPF - wpf

Can any one help me to get DynamicResource Binding in WPF by code?
I have set binding Like follow,
TextBlock Background={DynamicResource ColorA} Name="TB" in Xaml.
and i need to get the - TB's background is binded to "ColorA".
how can i access this DynamicResource Binding Expression in WPF by coding.
when i try to get TB.Background, it is white(#FFFFF..) or if i already given the value to the
Resorce key "ColorA" that will be given.
but i want to get this Binding Expression.
Thank in advance for your Help.
I think my Question wasn't clear.
I want to get What Reource Binding was done to the "TB" in Xaml by code.
But the aren't any TB.GetResourceReference. I Want some think like that.
Where that Binding expression is kept in WPF. I need to get the TB's BackgroundProperty was
Binded to Which( answer "ColorA") key?
thank a lot for sudden response.

You can use the FrameworkElement.SetResourceReference method:
MSDN: http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.setresourcereference.aspx
Provided your xaml has this:
<TextBlock x:Name="TB">
You can write this in the code behind:
TB.SetResourceReference(BackgroundProperty, "ColorA");

You can use this:
YourControl.Style = this.FindResource(NameOfYourStyleForThisControl) as Style;

Related

Q: Update viewmodel property from a listview binded textbox

i've got a textbox which text is binded to a listview selecteditem as follows:
<TextBox x:Name="txtAdditional" Width="300" Text="{Binding ElementName=lstPersons, Path=SelectedItem.Additional, Mode=OneWay}" />
Now i like to implement a mechanism to add new items to the listview using this textbox to get the actual data. So i would like to bind the text of the textbox to a property of the viewmodel so it can be processed by a command.
So it the textboxes text has to be binded to the listviews selecteditem and additionally to a property of my viewmodel.
I've searched around and found some approaches but i can't help to think that there should be some simpler mechanism to archive this goal.
What I found by now:
Using MultiBindung with some kind of ValueConverter? So it seems to me that this is primary for displaying and not for updating a viewmodels property.
The use of some selfdefined custom control?
The use of an BindingProxy with in- and out-dependencyproperties like in Impossible WPF Part 1: Binding Properties?
Is there another, simpler solution to this or would i have to use one of those above?
And if one should use one of those approaches, which one whould you choose?
I can't help but thinking that this issue hasn't been given some thoughts already, using an mvvm pattern?! ;-)
yes thanks to the hint from BionicCode.
We, or better I should have to think the MVVM concept out. Of course no - or at least as little code behind as possible.
I added the property "selectedPerson" to my viewmodel and bound the SelectedItem of the listview to this property. So the object related properties were at hand directly through the "selected" object in my viewmodel and there was no need anymore to access the textbox content at all.
Thanks to BionicCode for the hint!

Binding in mapping converter

I try to use MappingConverter (existed in our appication and worked nice) in this way:
<converters:MappingConverter x:Key="RewardTypeToSymbolConverter"
ElseMappingValue="BlaBla">
<converters:MappingEntry FromValue="{x:Static loc:SomeEnum.Value}"
ToValue="{Binding SomeStringInViewModel}" />
</converters:MappingConverter>
I get no exceptions, but my TextBlock show nothing. Breakpoint within a converter shows that ToValue property is NULL (but SomeStringInViewModel isn't).
Do anybody have some idea how can I use binding like this within a converter? Or using binding in resource is impossible?
Because Converters aren't in any tree, the DataBinding of the ToValue will not work.
Have look at Josh Smiths DataContext-Bridge-Pattern.
Converters are not in any tree, be it logical or visual. There should be no DataContext at all, if you want to do any kind of binding there you should specify a source (RelativeSource will of course not work) in addition to the path.
In any case, have a look at the output window of Visual Studio, the binding errors displayed there often help find the problem. Also see this article on debugging bindings.
It sounds like your DataContext is incorrect
I would recommend using a tool like Snoop to figure out what your DataContext is

WPF Update Binding when Bound directly to DataContext w/ Converter

Normally when you want a databound control to 'update,' you use the "PropertyChanged" event to signal to the interface that the data has changed behind the scenes.
For instance, you could have a textblock that is bound to the datacontext with a property "DisplayText"
<TextBlock Text="{Binding Path=DisplayText}"/>
From here, if the DataContext raises the PropertyChanged event with PropertyName "DisplayText," then this textblock's text should update (assuming you didn't change the Mode of the binding).
However, I have a more complicated binding that uses many properties off of the datacontext to determine the final look and feel of the control. To accomplish this, I bind directly to the datacontext and use a converter. In this case I am working with an image source.
<Image Source="{Binding Converter={StaticResource ImageConverter}}"/>
As you can see, I use a {Binding} with no path to bind directly to the datacontext, and I use an ImageConverter to select the image I'm looking for. But now I have no way (that I know of) to tell that binding to update. I tried raising the propertychanged event with "." as the propertyname, which did not work.
Is this possible? Do I have to wrap up the converting logic into a property that the binding can attach to, or is there a way to tell the binding to refresh (without explicitly refreshing the binding)?
Any help would be greatly appreciated.
Thanks!
-Adam
The workaround here was to add a property to my object (to be used as the datacontext) called "Self" , which simply returned
public Object Self { get { return this; }}
Then in the binding I used this property:
<Image Source="{Binding Path=Self, Converter={StaticResource ImageConverter}}"/>
Then when I call
PropertyChanged(this, new PropertyChangedEventArgs("Self"))
it works like a charm.
Thanks all.
I don't believe there is a way of accomplishing exactly what you need with your current converter. As you mentioned, you could do the calculation in your ViewModel, or you could change your converter into an IMulitValueConverter.
From your specific scenario (the converter tied to a ViewModel class, and a few of its properties), I would lean towards implementing the logic in the ViewModel.
Hmm, you don't show the full implementation. But I think it should update, if the value bound to the GUI provides the PropertyChanged-Event.
Regards

WPF Styles Question

A simple question which I can't find the answer on the web for some reason...
I want to place the content to a ResourceDictionary:
<TextBlock
Style="{StaticResource HomePageTextStyle}">
<LineBreak/>
Hello<LineBreak/>
<Bold>World!</Bold>
<LineBreak/>
</TextBlock>
The best I could do was:
<s:String x:Key="HomePageTextContent">
Hello World!
</s:String>
Which stripped all the formatting from the content :( Help please~ Thanks in advance. Oh, and If you can recommend a nice reference for using WPF Styles, it would be great~ Thanks!
I'd say you want to use data-binding instead of applying a style, since you are putting content in the TextBlock not changing the appearance of the TextBlock itself, e.g. drawing a border around it.
According to MSDN: TextBlock supports the hosting and display of Inline flow content elements. To be more precise, the content of the TextBlock in your first code block becomes a InlineCollection in the Inlines property of the TextBlock. Unfortunately the Inlines property isn't a dependency property so we can't bind data to it. The Text property, on the other hand, is a dependency property but doesn't allow anything other than a String.
To make a long story short, I don't think you can achieve what you want using pure XAML.

Is there a way to convert WPF binding markup to and from an instance of the binding class?

For example I have the following binding markup
Text="{Binding Path=FirstName}"
pretty simply but it could be much more complex, I need to be able to parse this markup and get it into some objectified form such as an instance of the Binding class.
Something that could work in reverse, an instance of the binding class to spit out the markup would be great as well.
I know such a thing must exist in the framework but I dont know where/what class.
I have looked at XamlReader but was unable to get it working because in this case I am missing context as I am only working with bits of the project and not the whole.
You can get the Binding object using GetBindingExpression, for example if you have:
<TextBlock Name="MyTextBlock" Text="{Binding Name}"/>
You can use:
BindingExpression expr = BindingExpression.GetBindingExpression(MyTextBlock, TextBlock.TextProperty);
Binding bindingObject = expr.ParentBinding;
To use XAMLReader you have to surround it with a valid root. then this shoudl work.

Resources