你可以使用data-binding
当您在顶部文本框中键入内容时,此处显示下方文本框中的文本。
像这样更改 ContentDialog Xaml:
<StackPanel>
<TextBlock Text="Map Name: "/>
<TextBox x:Name="txtbxMapName" Width="300" HorizontalAlignment="Left" Text="{Binding InputString,Mode= TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<TextBlock Text="Default Zoom Level: "/>
<ComboBox x:Name="cmbxCre8MapZoomLevels" Width="100" HorizontalAlignment="Left"/>
<TextBlock Text="Map Notes: "/>
<TextBox x:Name="txtbxMapNotes" Width="300" Height="300" HorizontalAlignment="Left" Text="{Binding InputString, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" />
</StackPanel>
在后面的代码中,创建一个名为 ViewModel 的新类:
public class ViewModel: INotifyPropertyChanged
{
private string _inputString;
public string InputString
{
get { return _inputString; }
set
{
_inputString = value;
RaisePropertyChanged("InputString");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(string name)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
}
在隐藏代码中:
public ViewModel viewModel { get; set; }
public MainPage()
{
this.InitializeComponent();
viewModel = new ViewModel();
this.DataContext = viewModel;
}
我用MainPage来测试。您可以将代码添加到应用程序的真实页面中。
您可能会对以下内容感到困惑INotifyPropertyChanged interface
和Binding markup extension
。
请参阅以下文档以获取有关数据绑定的更多信息:数据绑定概述 https://learn.microsoft.com/en-us/windows/uwp/data-binding/data-binding-quickstart and 数据深度绑定 https://learn.microsoft.com/en-us/windows/uwp/data-binding/data-binding-in-depth.
Update:
将 TextWrapping="Wrap" 添加到 TextBox 以使清除所有按钮不可见。
像这样:
<TextBox x:Name="txtbxMapNotes" Width="300" Height="300" TextWrapping="Wrap" HorizontalAlignment="Left" Text="{Binding InputString, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" />