AnnaBauer21
Member
- Joined
- Nov 29, 2024
- Messages
- 6
- Programming Experience
- 3-5
I created a small test project to simply illustrate my problem.
The following process:
XAML
Code Behind
In Event
I hope you have an idea.
I know it doesn't correspond to MVVM, but I'm currently trying to get it to work.
Thank you all
Kind regards
The following process:
- I have a TextBlock in the XAML with a binding to a RelativSource.
- In the code behind, highlighting information is added to the Inlines of the TextBlock
- Because of DataGrid with VirtualizingStackPanel.VirtualizationMode="Recycling" the result of GetBindingExpression(TextBlock.TextProperty) is null in the event DataGrid.OnLoadingRow but also in the event DataGridRow.Loaded
- Now I tried to reset the binding
- But this replaces the text in the TextBlock with the default value ("") instead of using the correct text based on the binding.
XAML
XML:
<StackPanel Tag="{Binding Path=MissionName, Converter={StaticResource DebugConverter}}">
<TextBlock
Text="{Binding Path=Tag, RelativeSource={RelativeSource AncestorType=StackPanel}" />
</StackPanel>
Code Behind
C#:
// Save data for binding
var exp = txtBlock.GetBindingExpression(TextBlock.TextProperty);
txtBlock.Tag = new Tuple<RelativeSource, PropertyPath>(exp.ParentBinding.RelativeSource, exp.ParentBinding.Path)
var inlines = new List<Inline>();
inlines.Add(new Run(text.Substring(0, 1)));
inlines.Add(new Run(text.Substring(1, 2))
{
Background = filterData.SelectionColor,
Foreground = filterData.ForegroundColor
});
inlines.Add(new Run(text.Substring(3, 1)));
txtBlock.Inlines.Clear();
txtBlock.Inlines.AddRange(inlines);
In Event
C#:
var tagData = (Tuple<RelativeSource, PropertyPath>) txtBlock.Tag;
var binding = new Binding { RelativeSource = tagData.Item1, Path = tagData.Item2};
txtBlock.SetBinding(TextBlock.TextProperty, binding); // <- txtBlock.Text now is ""
// This call says now, IsDirty="true" & Status="Unattached"
var exp = txtBlock.GetBindingExpression(TextBlock.TextProperty);
I hope you have an idea.
I know it doesn't correspond to MVVM, but I'm currently trying to get it to work.
Thank you all
Kind regards