如何使用WPF一次选择和更改多个控件的值?

时间:2014-11-12 16:02:37

标签: c# wpf mvvm wpf-controls

我是WPF的新手,我想选择多个控件(假设我用鼠标绘制一个矩形并选择此形状内的控件)并将其值更改为gheter。我怎样才能做到这一点?顺便说一下,我正在使用MVVM。

在下面的示例中,我有3个滑块,我希望能够选择它们,这样我就可以使用键盘一次控制所有这些滑块。我知道我不能同时将键盘焦点设置为3个。我怎样才能完成这项任务?

    <Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Slider IsSnapToTickEnabled="True" Grid.Column="0" Ticks="{Binding Slider1Values}" Value="{Binding Slider1Selected}" Orientation="Vertical" HorizontalAlignment="Center" Margin="10"/>
    <Slider IsSnapToTickEnabled="True" Grid.Column="1" Ticks="{Binding Slider2Values}" Value="{Binding Slider2Selected}" Orientation="Vertical" HorizontalAlignment="Center" Margin="10"/>
    <Slider IsSnapToTickEnabled="True" Grid.Column="2" Ticks="{Binding Slider3Values}" Value="{Binding Slider3Selected}" Orientation="Vertical" HorizontalAlignment="Center" Margin="10"/>
</Grid>

1 个答案:

答案 0 :(得分:0)

KeyUp事件中(或您选择的任何事件)修改所有3个绑定的值:

    private void Window_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == (Keys.Up)) {
            newValue = oldValue + 10;
        } else if (e.KeyCode == (Keys.Down)) {
            newValue = oldValue - 10;
        }

        this.Slider1Selected = newValue;
        this.Slider2Selected = newValue;
        this.Slider3Selected = newValue;
    }