在 WPF 中的视图中更改时,不会在 ViewModel 中更新文本框值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17692847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Text box value is not updated in ViewModel when changing in the view in WPF
提问by Dev
When I change the text box value, it is not updated in the view model.
当我更改文本框值时,它不会在视图模型中更新。
This is my text box xaml in the view
这是我在视图中的文本框 xaml
<TextBox Height="23" HorizontalAlignment="Left" Margin="153,65,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" Text="{Binding Path=InstallPath, Mode=TwoWay}"/>
Full xaml for the view,
视图的完整 xaml,
<Window.Resources>
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
</Window.Resources>
<Grid>
<Button Content="Configure Logger" Height="44" HorizontalAlignment="Left" Margin="402,125,0,0" Name="button1" VerticalAlignment="Top" Width="108" Click="button1_Click" />
<Button Content="Load DB" Height="43" HorizontalAlignment="Left" Margin="402,200,0,0" Name="button3" VerticalAlignment="Top" Width="108" Click="button3_Click" />
<TextBlock Height="23" HorizontalAlignment="Left" Margin="46,36,0,0" Name="textBlock1" Text="SQL Server" VerticalAlignment="Top" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="153,36,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" TextChanged="textBox1_TextChanged" />
<GroupBox Header="DB Names" HorizontalAlignment="Left" Margin="54,114,0,0" Name="groupBox1" VerticalAlignment="Top" >
</GroupBox>
<TextBlock Text="Test bootstrapper application." Margin="10" FontSize="18" HorizontalAlignment="Center" Foreground="Red" VerticalAlignment="Top" />
<TextBlock Height="23" HorizontalAlignment="Left" Margin="46,65,0,0" Name="textBlock2" Text="Installation Path" VerticalAlignment="Top" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="153,65,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" Text="{Binding Path=InstallPath, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<Ellipse Height="100" Width="100" HorizontalAlignment="Center" VerticalAlignment="Center" StrokeThickness="6" Margin="278,129,216,112"
Visibility="{Binding Path=IsThinking, Converter={StaticResource BooleanToVisibilityConverter}}">
<Ellipse.Stroke>
<LinearGradientBrush>
<GradientStop Color="Red" Offset="0.0"/>
<GradientStop Color="White" Offset="0.9"/>
</LinearGradientBrush>
</Ellipse.Stroke>
<Ellipse.RenderTransform>
<RotateTransform x:Name="Rotator" CenterX="50" CenterY="50" Angle="0"/>
</Ellipse.RenderTransform>
<Ellipse.Triggers>
<EventTrigger RoutedEvent="Ellipse.Loaded">
<BeginStoryboard>
<Storyboard TargetName="Rotator" TargetProperty="Angle">
<DoubleAnimation By="360" Duration="0:0:2" RepeatBehavior="Forever" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Ellipse.Triggers>
</Ellipse>
<StackPanel Orientation="Horizontal" VerticalAlignment="Bottom" HorizontalAlignment="Right">
<Button Content="Install" Command="{Binding Path=InstallCommand}" Visibility="{Binding Path=InstallEnabled, Converter={StaticResource BooleanToVisibilityConverter}}" Margin="10" Height="20" Width="80"/>
<Button Content="Uninstall" Command="{Binding Path=UninstallCommand}" Visibility="{Binding Path=UninstallEnabled, Converter={StaticResource BooleanToVisibilityConverter}}" Margin="10" Height="20" Width="80"/>
<Button Content="Exit" Command="{Binding Path=ExitCommand}" Margin="10" Height="20" Width="80" />
</StackPanel>
</Grid>
ViewModel
视图模型
private string installPath;
public string InstallPath
{
get { return installPath; }
set
{
installPath = value;
RaisePropertyChanged("InstallPath");
}
Method which consumes the text box value in WIX Bootstrapper
在 WIX Bootstrapper 中使用文本框值的方法
protected override void Run()
{
MainViewModel viewModel = new MainViewModel(this);
BootstrapperDispatcher = Dispatcher.CurrentDispatcher;
MainView view = new MainView();
view.DataContext = viewModel;
this.Engine.Log(LogLevel.Verbose, "My text input is: " + view.textBox2.Text);
viewModel.Bootstrapper.Engine.StringVariables["MyBurnVariable1"] = viewModel.InstallPath;
}
viewModel.InstallPath is empty even though I change the value in textbox, Do I miss something?
即使我更改了文本框中的值,viewModel.InstallPath 也是空的,我错过了什么吗?
I am just following the below WIX Bootstrapper example explained in the below link, http://bryanpjohnston.com/2012/09/28/custom-wix-managed-bootstrapper-application/
我只是按照以下链接中解释的以下 WIX Bootstrapper 示例进行操作, http://bryanpjohnston.com/2012/09/28/custom-wix-managed-bootstrapper-application/
回答by Jehof
Your bindings and setups seems ok with the code you have provided.
您的绑定和设置对于您提供的代码似乎没问题。
You should note that the default UpdateSourceTriggerof the TextBoxis LostFocus, so that your property is only updated when the TextBox lost focus.
您应该注意UpdateSourceTrigger的TextBox是LostFocus的默认值,因此您的属性仅在 TextBox 失去焦点时更新。
You can change the UpdateSourceTriggerto PropertyChanged, so that every time Text is written in the TextBox your property gets updated.
您可以将 更改UpdateSourceTrigger为PropertyChanged,以便每次在 TextBox 中写入 Text 时,您的属性都会更新。
You set up the UpdateSourceTrigger in the binding as follows
您在绑定中设置 UpdateSourceTrigger 如下
<TextBox Text="{Binding Path=InstallPath, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
回答by Vishal
You need to use UpdateSourceTrigger=PropertyChangein order to reflect your view change Back into ViewModel.
您需要使用UpdateSourceTrigger=PropertyChange以将您的视图更改反映回 ViewModel。
<TextBox Text="{Binding Path=InstallPath,Mode=TwoWay,UpdateSourceTrigger=PropertyChange}"/>

