How to populate fields in WPF Dictionary.xaml from CodeBehind

This forum is meant for questions and discussions about the X# language and tools
Post Reply
ic2
Posts: 2008
Joined: Sun Feb 28, 2016 11:30 pm
Location: Holland

How to populate fields in WPF Dictionary.xaml from CodeBehind

Post by ic2 »

Although this part of my project is not X# but C# the question is more WPF based so I hope that someone having done this can help me understand.

I start this question with the remark that Syncfusion sent me a full working program where I can modify the lay-out of their libabry's Kanban cards and also tooltips on it.

For that they simply have this code in the MainWindow code behind:

Code: Select all

            kanban.ToolTipTemplate = new DataTemplate()
            {
                VisualTree = new FrameworkElementFactory(typeof(KanbanToolTipView))
            };
and a public class KanbanToolTipView with code to change the layout and populate fields. Great service of their support and I will check it out and use it. But I would still like to know why everything I tried before did not work.

When I add a tooltip to a WPF XAML page, it works and I can change the lay-out. But I started from a Syncfusion sample where everything is created from code, there's only a basic MainWindow.XAML.

So I found a possible solution by creating a Dictionay.xaml, as follows:

Code: Select all

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <Style x:Key="{x:Type ToolTip}" TargetType="ToolTip">
		<StackPanel>
		<TextBlock Text="This works"></TextBlock>
		</StackPanel>
	</Style>
</ResourceDictionary>
(I omitted all the lay out properties in this sample). What I obviously want is one or more controls to fill with a value from my codebehind. But although I see the tooltip with all lay-out settings and the preset text "This works" none of the attempts to fill a value from the code behind works. E.g.:


1 Using a converter

Code: Select all

<TextBlock Text="{Binding ElementName=txtValue, Path=Text, Converter={StaticResource YourConverter}}"></TextBlock>
and

Code: Select all

public class YourConverter: IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
			return "XXX";
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
} 
ends with a Runtime error Cannot find resource name YourConverter

2 I can define something like public class MyData : INotifyPropertyChanged with Get/Set and PropertyChangedEventHandler etc. but it's not clear what I need to do in this textblock to trigger that event.
At least:
<TextBlock FontFamily="Tahoma" FontSize="11" Text="{Binding Path=MainWindow.cTest}" Foreground="#FFFFFFFF" />
is not enough to show the value of a public variable cTest

3 I found this code and named the Textblock TT

Code: Select all

		var dictionary = new ResourceDictionary
		{
		Source = new System.Uri("Dictionary.xaml", System.UriKind.Relative)
		};

		if (dictionary["TT"] is TextBlock ttb)
		{
		ttb.Text = "Your tooltip text from codebehind";
		}
but thew condition

Code: Select all

if (dictionary["TT"] is TextBlock ttb)		
is not met so it doesn't work either (and I don't even understand what this line is supposed to do)

For xxx in Text="{TemplateBinding xxx"} I can select the following options:
TemplateBinding.jpg
but these do not seemed to be documented together so I can't find what to do with it.

As said, I can no doubt solve the actual problem with Syncfusion's sample code. But I still wonder why none of the above solutions work. If anyone can comment on that, I would appreciate that.

Dick
User avatar
wriedmann
Posts: 4104
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Re: How to populate fields in WPF Dictionary.xaml from CodeBehind

Post by wriedmann »

Hi Dick,
please forget all the strange dictionaries and converters.
You have to use binding to make that work.
I'm using code like this:

Code: Select all

<DataTemplate xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> )
<TextBlock Text="{Binding MyTooltipText}"/>
</DataTemplate>
But of course you can put any control inside that DataTemplate.

Wolfgang
P.S. I'm using that in a SyncFusion DataGrid
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
ic2
Posts: 2008
Joined: Sun Feb 28, 2016 11:30 pm
Location: Holland

Re: How to populate fields in WPF Dictionary.xaml from CodeBehind

Post by ic2 »

Hello Wolfgang,
wriedmann wrote: Tue Mar 10, 2026 6:59 pm please forget all the strange dictionaries and converters.
You have to use binding to make that work.
The dictionary seems to be a good solution to define a tooltip-lay-out which directly works. But the issue is that none of the binding (or other) solutions to fill the textblock I have tried work. The tooltip shows empty.

I have used binding earlier a few times. I always hate it because it is impossible to debug. If nothing happens you cannot see why.

What is your codebehind for your MyTooltipText Also note that my program does not use the XAML to define the lay-out, this is done from code. In other programs with XAML defining the lay-out adding a tooltip is much easier.

Dick
User avatar
wriedmann
Posts: 4104
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Re: How to populate fields in WPF Dictionary.xaml from CodeBehind

Post by wriedmann »

Hi Dick,
I'm always defining my windows in code, never using XAML.
MyTooltipText is a property in the viewmodel that fills the grid, or better: the datasource of the grid is an ObservableCollection of ViewModels, and these have that property. So the tooltip depends on the underlying data.
Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Post Reply