Apply static resource style to UIElement created in converter - wpf

I am binding the ItemsSource of an ItemsControl to a Textbox and then using a converter to create UIElement objects based on the Text property of the Textbox. I also have a static resource style that I want to apply to the elements I create.
The problem I'm having is how to set the style of the items created in the converter to the static resource since I don't have access to the static resources in my converter class.

To use StaticResource in the Converter you could send the Style as ConverterParameter.
If you're already using the ConverterParameter you could make the Converter derive from DependencyObject, add a Dependency Property and set it to the Style on the creation of the Converter.
But the easiest solution is probably to use DynamicResource instead if you know the key of the resource.
The following Xaml
<UIElement Style="{DynamicResource styleKey}" />
is equivalent to the following C# code
myUIElement.SetResourceReference(StyleProperty, "styleKey");

Mission impossible! :) you can use ConverterParameter as your static resource, I think. and all will be ok!

Related

WPF: How can I retrieve the Template that was created by a ContentTemplateSelector for a specific ContentPresenter?

In my WPF app, I'm using several ContentPresenters with a special MarkupExtension that requires access to the ContentPresenter's ContentTemplate property.
The MarkupExtension works very well, except that I just found out that if a ContentPresetner uses a ContentTemplateSelector, it doesn't set its own ContentPresenter property: rather, both the ContentPresenter and the result of the ContentTemplateSelector get saved to a private variable of the ContentPresenter class, as can be seen here: link to .Net source code for ContentPresenter.
I figured out that I can call ContentTemplateSelector.SelectTemplate() again and get the template, or keep a dictionary of selected templates inside the ContentTemplateSelector so that I can fetch the template that was geneatedfor each element, but is there a better way to do this?

Is it possible to use binding inside other binding?

Is it possible to write something like this
<TextBlock Text="{Binding Path=TextSource, StringFormat='{Binding Path=StringFormat}' }"
Or the single way is to have three properties: one for some value and other for string presentation of this value, third for format string. In this case TextBox binds with string representation of value. String presentation changes when format string changes.
Yes, it is possible in general and no for your case it is not possible because StringFormat is not Dependency Property.
Binding only works on Dependency Properties.
If you wish that to work create a resource dictionary of type Freezable and let it inherit the actual DataContext. Futhermore use StaticResource extension to set StringFormat in Binding.
StringFormat is not DependencyProperty but it doest accept {StaticResource someKey}.
It's a workaround. But it would work.
Another alternative solution would be attached property.
Attached properties are bindable. You would need to listen to property changed event of your attached property and change the StringFormat inside the handler.

How to get rid of using ElementName in WPF bindings in XAML

I am building a WPF application and I have some DependencyProperties in my window's codebehind (actually a big bunch of them). I want to bind a textbox to one of these string values. If I use {Binding ObjectName} it just doesn't work and it complains about not finding the property in the output. If I use {Binding ObjectName, ElementName=window} (where window is my Window's instance name), it works. But I have lots of bindings and I don't want to use the ElementName property each time. Is there any shortcut that will default all the element names to the window objects, as all of my bindings have the same element?
Thanks,
Can.
The default source of a binding is FrameworkElement.DataContext so you have to set the DataContext property of your window to the instance of your window e.g. DataContext = this;

how to use style defined in xaml resource dictionary?

I have a a IMultiValueConverter that dynamically creates TextBlock controls. The issue is that it has no styles.
How can I tell my new TextBlock to use a style that was defined in my XAML resource dictionary?
See the following question: how to use DynamicResource in the code behind?
Use SetResourceReference, it's equivalent to use DynamicResource in Xaml
So if your Style has the Key myTextBlockStyle
TextBlock textBlock = new TextBlock();
textBlock.SetResourceReference(FrameworkElement.StyleProperty, "myTextBlockStyle");
I have never tried this before, and depending on what your converter is doing, I think if your XAML resource dictionary is external, then link it into the Window where you are displaying the TextBlocks:
<Window.Resources>
<ResourceDictionary Source="[the path to the resource dictionary]"/>
</Window.Resources>
Then in your textblocks, ensure they have the Style attached that is defined in the resource dictionary. If the textblocks are being created in code behind I believe you should be able to use FindResource to locate the style that is linked in by the resource dictionary. Then do something like this:
textBlock1.Style = (Style)FindResource("myTextBlockStyle");

WPF Custom Control - Binding a template item to a Path

In a WPF Custom Control template, is there any way that I can do the following in XAML?:
var selItemText = this.GetTemplateChild("PART_SelectedItemText") as TextBlock;
var binding = new Binding("SelectedItem." + DisplayMemberPath);
binding.RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent);
selItemText .SetBinding(TextBlock.TextProperty, binding);
Note that the interesting part of this statement is the binding constructor - I am building up a path based on both some text I specify ("SelectedItem."), and the path provided by the user.
The consumer would use the control similar to:
<c:MyControl DisplayMemberPath="Description" />
short answer: no, it's not possible to get this entirely in xaml within the controltemplate
your possibilities are:
use what you have (possibly using attached properties / a behavior to make it more MVVM-like)
use a MultiBinding one binding to the "SelectedItem" the other to "DisplayMemberPath" and your MultiValueConverter using Reflection to reflect down the DisplayMemberPath (may be a bit ugly)
create a class that inherits from Binding and exposes Properties that you can bind the DisplayMemberPath to and changes the underlying Binding (read here for how you can do this) (complicated)
use Reflection to instantiate a MS.Internal.Data.DisplayMemberTemplateSelector / build something similar
think about if your design is right. Other than your Control being some kind of ItemsControl (if that was the case you should inherit from ItemsControl and use the DisplayMemberPath you get there), I don't see why you shouldn't use a Binding on the outside like <c:MyControl DisplayMember="{Binding SelectedItem.Description}" /> and in your ControlTemplate use a TemplateBinding to bind to "DisplayMember"
You can split it to two different bindings. Have SelectedItem binds to a toplevel control of TextBlock and TextBox.Text bind to DisplayMemberPath as TemplateBinding.

Resources