C
C#4mo ago
Natriss

Binding and DependencyProperty

Generic.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:ValidationTest"
xmlns:controls="using:ValidationTest.Controls">

<Style TargetType="controls:ValidationTextBox">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="controls:ValidationTextBox">
<Grid ColumnSpacing="12">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>

<TextBox x:Name="TextBox" Grid.Column="0" Header="Test" Text="{TemplateBinding Text}" Description="Test" PlaceholderText="Test" />
<FontIcon x:Name="ErrorIcon" Grid.Column="1" Glyph="&#xE814;" Foreground="Orange" Visibility="Visible" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:ValidationTest"
xmlns:controls="using:ValidationTest.Controls">

<Style TargetType="controls:ValidationTextBox">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="controls:ValidationTextBox">
<Grid ColumnSpacing="12">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>

<TextBox x:Name="TextBox" Grid.Column="0" Header="Test" Text="{TemplateBinding Text}" Description="Test" PlaceholderText="Test" />
<FontIcon x:Name="ErrorIcon" Grid.Column="1" Glyph="&#xE814;" Foreground="Orange" Visibility="Visible" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
ValidationTextBox.cs
public sealed class ValidationTextBox : Control
{
public ValidationTextBox()
{
this.DefaultStyleKey = typeof(ValidationTextBox);
}

public string Text
{
get => (string)GetValue(TextProperty);
set => SetValue(TextProperty, value);
}

public static readonly DependencyProperty TextProperty = DependencyProperty.Register(nameof(Text), typeof(string), typeof(ValidationTextBox), new PropertyMetadata(default(string)));
}
public sealed class ValidationTextBox : Control
{
public ValidationTextBox()
{
this.DefaultStyleKey = typeof(ValidationTextBox);
}

public string Text
{
get => (string)GetValue(TextProperty);
set => SetValue(TextProperty, value);
}

public static readonly DependencyProperty TextProperty = DependencyProperty.Register(nameof(Text), typeof(string), typeof(ValidationTextBox), new PropertyMetadata(default(string)));
}
For some reason he doesn't do these Mode=TwoWay, UpdateSourceTrigger=PropertyChanged. <controls:ValidationTextBox Text="{x:Bind ViewModel.Text, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" /> What am I doing wrong here? I followed this https://learn.microsoft.com/en-us/windows/apps/winui/winui3/xaml-templated-controls-csharp-winui-3
0 Replies
No replies yetBe the first to reply to this messageJoin