我在 WinUI 3 中遇到了一個問題ContentDialogs。我有一個面板打開ContentDialog. ContentDialog包含TextBox一個.
Panel 有一個屬性,該屬性應存盤已輸入TextBox到ContentDialog. 此外,TextBox最初應填寫面板中屬性的原始值
為了實作這一點,我嘗試將依賴屬性注入到對話框中,它是雙向系結到TextBoxUI 中的。
預期行為
我假設寫入的文本TextBox應該轉移到面板的屬性中。然而,這種情況并非如此。
當前行為
如果TextBox我更改TextBox.
這是為什么?
這是一個小草圖
設定草圖
代碼示例
面板中的依賴屬性(面板也注入此屬性)
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));
打開對話框的方法
private async void ShowTextBoxDialog()
{
var dialog = new TextBoxDialog()
{
TextBoxValue = PanelTextBoxValue,
};
dialog.XamlRoot = XamlRoot;
await dialog.ShowAsync();
}
對話框中的依賴屬性
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));
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}"/>
我所看到的
如果我在 Dialog 中更改 TextBox 的值,則TextBoxValue屬性會按應有的方式更新(在 set 方法中設定斷點)。但是PanelTextBoxValue沒有更新。
需要幫助請叫我!我有點無能為力,因為它在一個方向上起作用,但在另一個方向上不起作用。
uj5u.com熱心網友回復:
顯示的唯一傳輸值的代碼是TextBoxValue = PanelTextBoxValue將值從 Panel 復制一次到 Dialog。TextBoxValue dependencyProperty 和 PanelTextBoxValue dependencyProperty 之間沒有系結。
選項 1:簡單
...
await dialog.ShowAsync();
PanelTextBoxValue = dialog.TextBoxValue;
選項 2:實時同步
SetBinding(PanelTextBoxValueProperty, new System.Windows.Data.Binding
{
Path = new PropertyPath(TextBoxDialog.TextBoxValueProperty),
Source = dialog
});
await dialog.ShowAsync();
BindingOperations.ClearBinding(this, PanelTextBoxValueProperty);
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/533322.html
上一篇:我用ILSpy反編譯了一個dotnet.dll,但xaml檔案有一些問題
下一篇:根據UWP中的型別選擇資料模板
