4

当我更改文本框值时,它不会在视图模型中更新。

这是我在视图中的文本框 xaml

<TextBox Height="23" HorizontalAlignment="Left" Margin="153,65,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" Text="{Binding Path=InstallPath, Mode=TwoWay}"/>

视图的完整 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>

视图模型

 private string installPath;
    public string InstallPath
    {
        get { return installPath; }
        set
        {
            installPath = value;
            RaisePropertyChanged("InstallPath");                
        }

在 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 也是空的,我错过了什么吗?

我只是按照以下链接中解释的以下 WIX Bootstrapper 示例, http://bryanpjohnston.com/2012/09/28/custom-wix-managed-bootstrapper-application/

4

2 回答 2

9

您提供的代码似乎可以使用您的绑定和设置。

您应该注意,默认值为UpdateSourceTriggerLostFocus 因此您的属性仅在 TextBox 失去焦点时更新。TextBox

您可以将 更改UpdateSourceTriggerPropertyChanged,以便每次将 Text 写入 TextBox 时,您的属性都会更新。

您在绑定中设置 UpdateSourceTrigger,如下所示

<TextBox Text="{Binding Path=InstallPath, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/> 
于 2013-07-17T06:54:33.720 回答
2

您需要使用UpdateSourceTrigger=PropertyChange以将您的视图更改反映回 ViewModel。

<TextBox Text="{Binding Path=InstallPath,Mode=TwoWay,UpdateSourceTrigger=PropertyChange}"/>
于 2013-07-17T06:57:45.540 回答