Home > Enterprise >  Dependency Property in ContentDialog works only in one direction
Dependency Property in ContentDialog works only in one direction

Time:11-12

I got a problem concerning ContentDialogs in WinUI 3. I have a Panel opening a ContentDialog. The ContentDialog contains a TextBox.

The Panel has a property that should store the Text that has been typed into the TextBox of the ContentDialog. Also the TextBox should initially be filled with the original value of the property from the Panel

To achieve that I tried to inject a dependency property into the Dialog, which is TwoWay binded to the TextBox in the UI.

Expected behavior

I assumed that the Text written into the TextBox should be transferred to the Property of the Panel. However this is not the case.

Current behavior

The TextBox gets its initial value from the property but the property is not updated, if I change the Text in the TextBox.

Why is that?

Here is a small sketch

Sketch of the setup

Code example

Dependency property in panel (the panel gets this property injected as well)

public string? PanelTextBoxValue
{
    get => (string?)GetValue(PanelTextBoxValueProperty);
    set => SetValue(PanelTextBoxValueProperty, value);
}

public static readonly DependencyProperty PanelTextBoxValueProperty =
    DependencyProperty.Register(
        nameof(PanelTextBoxValue),
        typeof(string),
        typeof(Panel),
        new PropertyMetadata(null));

Method to open the dialog

private async void ShowTextBoxDialog()
{
    var dialog = new TextBoxDialog()
    {
        TextBoxValue = PanelTextBoxValue,
    };
    dialog.XamlRoot = XamlRoot;
    await dialog.ShowAsync();
}

Dependency property in Dialog

public string? TextBoxValue
{
    get => (string?)GetValue(TextBoxValueProperty);
    set => SetValue(TextBoxValueProperty, value);
}

public static readonly DependencyProperty TextBoxValueProperty =
    DependencyProperty.Register(
        nameof(TextBoxValue),
        typeof(string),
        typeof(TextBoxDialog),
        new PropertyMetadata(null));

Corresponding part of the Dialog.xaml

<TextBlock Grid.Column="0" Grid.Row="0" Text="Line 1:" VerticalAlignment="Center"/>
<TextBox Name="Line1" Grid.Column="1" Grid.Row="0" Text="{x:Bind TextBoxValue, Mode=TwoWay}"/>

What I've seen

If I change the value of the TextBox in the Dialog the TextBoxValue property is updated as it should (set a breakpoint in the set method). However the PanelTextBoxValue is not updated.

let me know if you need anything else! I am a bit clueless because it works in one direction but not in the other.

CodePudding user response:

The only code shown which transfers the value is TextBoxValue = PanelTextBoxValue which copies the value once from Panel to Dialog. There is no Binding between TextBoxValue dependencyProperty and PanelTextBoxValue dependencyProperty.

Option 1: Simple

...
await dialog.ShowAsync();
PanelTextBoxValue = dialog.TextBoxValue;

Option 2: For real time syncing

SetBinding(PanelTextBoxValueProperty, new System.Windows.Data.Binding
{
    Path = new PropertyPath(TextBoxDialog.TextBoxValueProperty),
    Source = dialog
});
await dialog.ShowAsync();
BindingOperations.ClearBinding(this, PanelTextBoxValueProperty);
  • Related