我很难在文本框中显示变量。
主要尝试了解绑定(bind)在 XAML 中的工作方式。
我需要尝试在相关字段中显示变量 TextBoxFileName
和 TextBoxFilePath
。变量检索的信息存储在单独的 GlobalVariableStorage 类中。我不希望 TextBox
字段可编辑,因此我将它们设置为只读。我根本不希望用户能够编辑这些字段中的数据。如果您有其他显示方法的想法,请随时提出建议。
XAML
<Page.Resources>
<ResourceDictionary>
<ResourceDictionary.ThemeDictionaries>
<!-- Placeholder for the theme dictionary -->
</ResourceDictionary.ThemeDictionaries>
</ResourceDictionary>
</Page.Resources>
<Frame Background="{StaticResource CustomAcrylicDarkBackground}">
<StackPanel>
<TextBox Width="500" Header="File Name" PlaceholderText="Name Of File" IsReadOnly="True" Foreground="White" Text=""/>
<TextBox Width="500" Header="File Location" PlaceholderText="File Location" IsReadOnly="True" Foreground="White" Text=""/>
</StackPanel>
</Frame>
隐藏代码
public sealed partial class SettingsPage : Page
{
public SettingsPage()
{
this.InitializeComponent();
}
public class TextBoxDisplay
{
public string TextBoxFileName = GlobalVariables.FileName;
public string TextBoxFilePath = GlobalVariables.FilePath;
}
}
最佳答案
由于方法错误,您遇到了困难。
例如,您没有遵循 MVVM 模式。另外,您无需将 IsReadOnly
设置为 true,只需使用单向绑定(bind)即可。
<TextBox Text="{Binding TextBoxFileName,Mode=OneWay}"/>
为了正确理解和实现MVVM,我建议您阅读以下链接:MVVM for WPF 。虽然它是针对 WPF 的,但 UWP 与 WPF 非常相似,您不会遇到任何问题。
如果你想学习MVVM,我可以帮助你。只需在 Discord 上给我发消息即可:Red Wei#2396
玩得开心。
关于c# - 在文本框中显示变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52219414/