XAML 为也可以更改的文本框设置初始值
XAML set an initial value for a textbox that can also be changed
我的 XAML 中有一个显示当前工作目录的文本框。 XAML 代码如下所示:
<StackPanel x:Name="StackCurrentPath" Orientation="Vertical" Margin="25,25,25,5">
<StackPanel Orientation="Horizontal" Margin="0,5,0,0">
<TextBox materialDesign:HintAssist.Hint="Path of app*" materialDesign:HintAssist.HintOpacity="10" materialDesign:HintAssist.Background="White" materialDesign:ValidationAssist.Background="White" materialDesign:HintAssist.Foreground="#FF002655" Style="{StaticResource MaterialDesignOutlinedTextFieldTextBox}" x:Name="txtboxCurrentPath" Width="230" TextChanged="txtboxCurrentPath_TextChanged">
<Binding Path="CurrentPath" UpdateSourceTrigger="PropertyChanged" Mode="TwoWay" Source="{StaticResource MyConfigurations}" >
<Binding.ValidationRules>
<local:PathValidation ValidatesOnTargetUpdated="True"/>
</Binding.ValidationRules>
</Binding>
</TextBox>
</StackPanel>
</StackPanel>
后面的代码如下所示:
public partial class PathSection: Page
{
private string currentDirectoryPath = System.AppDomain.CurrentDomain.BaseDirectory;
public PathSection()
{
var currentDir = System.IO.Path.GetFullPath(System.IO.Path.Combine(currentDirectoryPath, @"..\Assets\"));
}
InitializeComponent();
txtboxCurrentPath.Text = currentDir;
private void txtboxCurrentPath_TextChanged(object sender, RoutedEventArgs e)
{
//some code here for binding the value to MyConfigurations.CurrentPath
}
}
我试图做到这一点,最初,文本框应该自己显示路径,但用户仍然可以根据需要对其进行更改。我遇到的问题是,当我进行更改并导航回此页面时,组件会重新初始化并且用户更改的路径会被覆盖。我怎样才能使初始值本身就是当前路径,但是用户仍然可以更改并且当他们回来时它不会被覆盖?
改变
txtboxCurrentPath.Text = currentDir;
至
txtboxCurrentPath.Text = string.IsNullOrEmpty(MyConfigurations.CurrentPath) ? currentDir : MyConfigurations.CurrentPath;
但我认为你的设计不正确
我的 XAML 中有一个显示当前工作目录的文本框。 XAML 代码如下所示:
<StackPanel x:Name="StackCurrentPath" Orientation="Vertical" Margin="25,25,25,5">
<StackPanel Orientation="Horizontal" Margin="0,5,0,0">
<TextBox materialDesign:HintAssist.Hint="Path of app*" materialDesign:HintAssist.HintOpacity="10" materialDesign:HintAssist.Background="White" materialDesign:ValidationAssist.Background="White" materialDesign:HintAssist.Foreground="#FF002655" Style="{StaticResource MaterialDesignOutlinedTextFieldTextBox}" x:Name="txtboxCurrentPath" Width="230" TextChanged="txtboxCurrentPath_TextChanged">
<Binding Path="CurrentPath" UpdateSourceTrigger="PropertyChanged" Mode="TwoWay" Source="{StaticResource MyConfigurations}" >
<Binding.ValidationRules>
<local:PathValidation ValidatesOnTargetUpdated="True"/>
</Binding.ValidationRules>
</Binding>
</TextBox>
</StackPanel>
</StackPanel>
后面的代码如下所示:
public partial class PathSection: Page
{
private string currentDirectoryPath = System.AppDomain.CurrentDomain.BaseDirectory;
public PathSection()
{
var currentDir = System.IO.Path.GetFullPath(System.IO.Path.Combine(currentDirectoryPath, @"..\Assets\"));
}
InitializeComponent();
txtboxCurrentPath.Text = currentDir;
private void txtboxCurrentPath_TextChanged(object sender, RoutedEventArgs e)
{
//some code here for binding the value to MyConfigurations.CurrentPath
}
}
我试图做到这一点,最初,文本框应该自己显示路径,但用户仍然可以根据需要对其进行更改。我遇到的问题是,当我进行更改并导航回此页面时,组件会重新初始化并且用户更改的路径会被覆盖。我怎样才能使初始值本身就是当前路径,但是用户仍然可以更改并且当他们回来时它不会被覆盖?
改变
txtboxCurrentPath.Text = currentDir;
至
txtboxCurrentPath.Text = string.IsNullOrEmpty(MyConfigurations.CurrentPath) ? currentDir : MyConfigurations.CurrentPath;
但我认为你的设计不正确