There is a data template. It binds data from a certain class containing the properties Name, Symbol, Value, Min, Max and Dimension. Binding of the first three properties is successful. Problems begin when using validation and a converter, as shown in the code. As I believe, the Xaml parser looks for the Min, Max and Dimension properties among the Value properties, and it is a string. Consequently, in Min, Max and Dimension drops Null. I tried to solve this problem by playing with RelativeSource, but to no avail. Any ideas?
<DataTemplate> <Border> <WrapPanel> <TextBlock Width="430" Height="30" Text="{Binding Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/> <TextBlock Width="60" Height="30" Text="{Binding Symbol, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/> <TextBox Width="100" Height="30"> <Binding Path="Value" UpdateSourceTrigger="PropertyChanged"> <Binding.ValidationRules> <calc:VarValidation> <calc:VarValidation.Params> <calc:ValidationParams Min="{Binding Min}" Max="{Binding Max}" /> </calc:VarValidation.Params> </calc:VarValidation> </Binding.ValidationRules> <Binding.Converter> <measuring:TemperatureConvert > <measuring:TemperatureConvert.myParameter> <Binding Path="Dimension" RelativeSource="{RelativeSource Mode=TemplatedParent}" diag:PresentationTraceSources.TraceLevel="High" /> </measuring:TemperatureConvert.myParameter> </measuring:TemperatureConvert> </Binding.Converter> </Binding> </TextBox> </WrapPanel> </Border> </DataTemplate>
As I understand it, the author of the article using this design
<Window.Resources> <FrameworkElement x:Key="DataContextBridge" /> </Window.Resources> <Window.DataContext> <Binding Mode="OneWayToSource" Path="DataContext" Source="{StaticResource DataContextBridge}" /> </Window.DataContext>
resets the root element of the data context into the data context of a certain FrameworkElement, after which it takes the data for the target element from the created FrameworkElement. But to assign a data context, it uses code:
this.DataContext = nums;
Can I do the same with Xaml markup? This is necessary because in my case a data template is used, the elements of which are repeated on the form many times, therefore, they do not have a name by which you can refer to the target element. And without explicitly setting the data context, it is null.
<TextBox.Resources> <FrameworkElement x:Key="DataContextBridge" /> </TextBox.Resources> <TextBox.DataContext> <!-- ΠΠ΄Π΅ΡΡ null. ΠΠ°ΠΊ ΠΈΡΠΏΡΠ°Π²ΠΈΡΡ? --> <Binding Mode="OneWayToSource" Path="DataContext" Source="{StaticResource DataContextBridge}" /> </TextBox.DataContext>