Binding in mapping converter - wpf

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

Related

WPF // MahApps.Metro // Caliburn.Micro // Flyout // HeaderedContentControl

Since a change in MahApps.Metro 1.5.0 the base element of a Flyout was changed from ContentControl to HeaderContentControl. Now the MVVM approach with Caliburn.Micro and this suggestion doesn't work anymore.
Has anybody else tried to solve this issue in a nice MVVM way?
While I haven't tried it myself but since the new Flyout control is now based on HeaderedContentControl it would be a simple logic choice to attempt to do this instead of the previous, which was based on ContentControl. Looks like the advantages of this change were the elimination of a couple of dependency properties...
<controls:FlyoutsControl.ItemTemplate>
<DataTemplate>
<HeaderedContentControl cal:View.Model="{Binding}" />
</DataTemplate>
</controls:FlyoutsControl.ItemTemplate>
The other thing that might come out of this that you create a Caliburn.micro convention to actually bind correctly to that HeaderedContentControl which wouldn't be all that different from the ContentControl variant was, by default in CM already. What is odd is that this HeaderedContentControl is derived from ContentControl in theory it should have been found correctly. Another thing to think about is this actually might also be related to Visual Tree where CM can't see it till it's in the tree, therefore can't bind it.
Content Control convention in CM source as a reference.
I've added posted a work around on GitHub.
It appears there's something going on with setting the ItemTemplate of the FlyoutsControl.

Current binding value

I'm writing markup extension. I have XAML like this
<TextBlock Text="{ui:Test SomeInfo}" />
and TestExtension with constructor taking one string argument. I'm getting "SomeInfo" string so everything is find. Now I want to nest extensions and write something like
<TextBlock Text="{ui:Test {Binding PropName}}" />
and it does not work as is. I had to add constructor which takes one argument of System.Windows.Data.Binding type.
Now I need to know
How should I retrieve a current value from the Binding object?
When should I do this? Should I subscribe to changes some way or ask for that value every time in ProvideValue method?
Update1 PropName should be resolved against DataContext of TextBlock.
Update2 Just found related question: How do I resolve the value of a databinding?
Bindings like this will not work because your MarkupExtension has no DataContext and it does not appear in the visual tree and i do not think you are supposed to interact with binding objects directly. Do you really need this extension? Maybe you could make do with the binding alone and a converter?
If not you could create a dedicated class which has bindable properties (by inheriting from DependencyObject), this however would still not give you a DataContext or namescope needed for ElementName or a visual tree needed for RelativeSource, so the only way to make a binding work in that situation is by using a Source (e.g. set it to a StaticResource). This is hardly ideal.
Also note that if you do not directly set a binding the ProvideValue method will only be called once, this means that even if you have a binding in your extension it may not prove very useful (with some exceptions, e.g. when returning complex content, like e.g. an ItemsControl which uses the binding, but you set the extension on TextBlock.Text which is just a string), so i really doubt that you want to use a MarkupExtension like this if the value should change dynamically based on the binding. As noted earlier: Consider converters or MultiBindings for various values instead.

Can you view XAML as "regular" .net code (c#\vb.net)?

There are times when I find some example XAML that I want\need to do in code (c#\vb.net).
I assume at some point the XAML becomes code, or at least IL.
So my questions:
Am I correct in assuming that XAML is converted to IL? (or if not IL what does it become?)
If the above is correct, when does XAML become IL (or whatever it becomes)?
Is there some way to see the XAML in as "code"
Thanks.
No, XAML does not compile into IL, that gets done at runtime. The best way to think about it is as a way to compose an application from components.
For the majority of things you can replicate in C# what you do in XAML, however there is a small number of things that is available in XAML that's not in C# and vice versa. Charles Petzold at some point said that ostensibly, XAML looks like XML, but it's actually not, it's a language of its own.
For example this XAML code:
<Grid>
<TextBlock Text="Something" />
</Grid>
Is equivalent to the following C# code. This will get done in C# at runtime and short of setting a breakpoint in a particular component's constructor, there isn't much you can do to figure out what executes at runtime.
var grid = new Grid();
grid.Children.Add(new TextBlock{Text = "Something"});
I am sure there is a solution to your problem, but not as an answer to this particular question. Can you give more details on your problem and we can help you understanding it.
WPF and Silverlight treat XAML differently; neither convert XAML to IL. WPF's markup compiler converts XAML to a compiled form called BAML that is a binary version of the XAML. Silverlight leaves the XAML as plain text (compressd in the .XAP) and parses it at runtime.
Is there some way to see the XAML in
as "code"
If you are talking about the hierarchy of controls in xaml, then you may use myControl.Parent.
You can "see in code" how the controls in xaml are nested. You will also get/set their properties.

Get DynamicResource Binding in 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;

WPF Databinding intellisense

Ok, so I have a general question about WPF. I've messed a little with ASP.NET MVC and in the markup it has intellisense on your viewmodel object so you don't mistype it( i love it).
Enter WPF, I love it, I'm utilizing the MVVM approach and one annoying thing that I have to do is make sure I'm binding correctly to my viewmodel. So I type something up in my xaml, then I either
1) if I'm lazy just run the app and click around
2) If i'm not lazy
a)Look at the current DataContext of the control I'm working with.
b)Go to that ViewModel class look at the properties
c)Find my property and then click back to my view and make sure that I spelled it correctly.
I believe that you can look at your trace output for incorrect bindings at run time, but is there something in the horizon to allow intellisense in xaml. The View needs to know about my ViewModel, so why not expose that in xaml.
I think it would be nice for the IDE to do that for me, meaning, when I type "{Binding " it should go up the visual tree find the first non-null DataContext and display the properties, let me select one and there you have it.
Am I missing something? Are there any alternatives. Any insight?
EDIT:
Regarding the comments below there is a way to set the datacontext at design time e.g.
<Window.Resources>
<DesignTimeData:DesignTimeCustomers x:Key="designTimeCustomersDS" />
</Window.Resources>
which is a snippet from this blog:
http://karlshifflett.wordpress.com/2008/10/11/viewing-design-time-data-in-visual-studio-2008-cider-designer-in-wpf-and-silverlight-projects/
All it does is reference a class within xaml.
So with that in place could not the designer then utilize reflection on the datacontext to give you all of the properties for that class?
VS 2013:
IntelliSense with resolved DataContext
IntelliSense with design time DataContext
So with that in place could not the
designer then utilize reflection on
the datacontext to give you all of the
properties for that class?
Resharper 6 has this feature and can show you members of the d:DataContext in the intellisense, if you set the d:DataContext on the View.
Visual Studio 2010 will support intellisense on Bindings. However, this is limited to the properties of the Binding object itself. For example after typing "{Binding " into the XAML editor I then get intellisense on the remaining properties (Path, ElementName, etc.).
I don't think the type of intellisense regarding the data context you specified would be possible as the data context is set at run-time. I've tried experimenting in VS 2010 by setting the data context directly in the constructor but did not have any luck.
According to :
http://blogs.msdn.com/b/visualstudio/archive/2013/08/09/xaml-editor-improvements-in-visual-studio-2013.aspx
Visual studio 2013 xaml Editor now is supporting IntelliSense for Data Binding
In Visual Studio 2010 there is a binding builder which you can enable
http://blogs.msdn.com/b/wpfsldesigner/archive/2010/06/14/how-to-enable-using-the-binding-builder-in-wpf-and-silverlight-applications.aspx
but you still don't get intellisense which is weird.

Resources