c# - 在文本框中显示变量

标签 c# xaml uwp uwp-xaml

我很难在文本框中显示变量。

主要尝试了解绑定(bind)在 XAML 中的工作方式。

我需要尝试在相关字段中显示变量 TextBoxFileNameTextBoxFilePath 。变量检索的信息存储在单独的 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/

相关文章:

c# - 无法在异步方法中隐式转换

c# - 在 Visual Studio 2010 加载项中访问命名空间时出现问题

c# - 如何在 WPF 中显示表格

c# - Windows Phone 导航按钮与屏幕分辨率重叠

xaml - 应用内日历上的周 View (通用 Windows 平台/UWP)

c# - 数据绑定(bind)到 ListView 内部的命令在 UWP/MVVM-Light 中不起作用

c# - 没有触发器的循环 Storyboard 动画

c# - Visual Studio SUO 文件破解应用程序

c# - 如何将命令绑定(bind)到 StackPanel 或 Grid 点击事件?

c# - XAML 文件中的不同基类