在 TextBox 中显示变量
Display variable within TextBox
我无法在文本框中显示变量。
主要是想了解 Bindings 在 XAML 中的工作方式。
我需要尝试在相关字段中显示变量 TextBoxFileName
和 TextBoxFilePath
。变量检索的信息存储在单独的 GlobalVariableStorage
class 中。我不希望 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,您只需使用单向绑定。
<TextBox Text="{Binding TextBoxFileName,Mode=OneWay}"/>
要正确理解和实现 MVVM,建议您阅读以下链接:MVVM for WPF。虽然它是针对WPF的,但是UWP和WPF非常相似,你不会遇到任何问题。
如果你想学MVVM,我可以帮你。只需在 Discord 上给我发消息:Red Wei#2396
玩得开心。
将两个只读属性添加到您的 SettingsPage
class:
public sealed partial class SettingsPage : Page
{
public SettingsPage()
{
this.InitializeComponent();
}
public string TextBoxFileName => GlobalVariables.FileName;
public string TextBoxFilePath => GlobalVariables.FilePath;
}
...并绑定到这些:
<TextBox Header="File Name" ... Text="{x:Bind TextBoxFileName}"/>
<TextBox Header="File Name" ... Text="{x:Bind TextBoxFilePath}"/>
我无法在文本框中显示变量。
主要是想了解 Bindings 在 XAML 中的工作方式。
我需要尝试在相关字段中显示变量 TextBoxFileName
和 TextBoxFilePath
。变量检索的信息存储在单独的 GlobalVariableStorage
class 中。我不希望 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,您只需使用单向绑定。
<TextBox Text="{Binding TextBoxFileName,Mode=OneWay}"/>
要正确理解和实现 MVVM,建议您阅读以下链接:MVVM for WPF。虽然它是针对WPF的,但是UWP和WPF非常相似,你不会遇到任何问题。
如果你想学MVVM,我可以帮你。只需在 Discord 上给我发消息:Red Wei#2396
玩得开心。
将两个只读属性添加到您的 SettingsPage
class:
public sealed partial class SettingsPage : Page
{
public SettingsPage()
{
this.InitializeComponent();
}
public string TextBoxFileName => GlobalVariables.FileName;
public string TextBoxFilePath => GlobalVariables.FilePath;
}
...并绑定到这些:
<TextBox Header="File Name" ... Text="{x:Bind TextBoxFileName}"/>
<TextBox Header="File Name" ... Text="{x:Bind TextBoxFilePath}"/>