IsDirectionReversed更改时,Thumb位置无效

时间:2015-07-30 15:28:15

标签: c# .net wpf wpf-controls

我在C#WPF应用程序中使用滑块控件,并希望为用户提供动态反转控件的功能。

我已经确定滑块控件具有内置的IsDirectionReversed属性,该属性适用于更改控件的方向(有效地将其最小值和最大值交换到控件的相对端)。但是,当属性在运行中更改时,拇指位置保持原样(在视觉上暗示不正确的值),但其值保持不变(如我所料)。

Example of required behaviour

(反转控件后,单击轨道上的任意位置会使拇指更新为当前值的+1或-1,但将拇指重新定位到正确的视觉位置)

当倒置属性发生变化时,是否有人可以建议强制拇指更新其位置的方法?

XAML

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        DataContext="{Binding RelativeSource={RelativeSource Self}}"
        Title="MainWindow" Height="139" Width="303">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="10"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="10"/>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="10"/>
        </Grid.ColumnDefinitions>

        <Slider Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2" Name="SliderControl" HorizontalAlignment="Left" VerticalAlignment="Top" Width="276" IsDirectionReversed="{Binding Inverted}" Maximum="100" Minimum="1" SmallChange="1"/>

        <TextBlock Grid.Row="2" Grid.Column="1" Text="Value:" HorizontalAlignment="Left" VerticalAlignment="Top" />
        <TextBlock Grid.Row="2" Grid.Column="2" Text="{Binding ElementName=SliderControl, Path=Value}" HorizontalAlignment="Left" VerticalAlignment="Top"/>

        <TextBlock Grid.Row="3" Grid.Column="1" Text="Inverted:" HorizontalAlignment="Left" VerticalAlignment="Top" />

        <TextBlock Grid.Row="3" Grid.Column="2" Text="{Binding ElementName=SliderControl, Path=IsDirectionReversed}" HorizontalAlignment="Left" VerticalAlignment="Top"/>
        <Button Grid.Row="4" Grid.Column="2" Content="Flip!" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
    </Grid>
</Window>

C#

public partial class MainWindow : Window, INotifyPropertyChanged
{
    public bool mSliderInverted = false;

    public bool Inverted
    {
        get
        {
            return mSliderInverted;
        }
    }

    public MainWindow()
    {
        InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        mSliderInverted = !mSliderInverted;
        OnPropertyChanged("Inverted");
    }

    protected void OnPropertyChanged(PropertyChangedEventArgs e)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
            handler(this, e);
    }

    protected void OnPropertyChanged(string propertyName)
    {
        OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

1 个答案:

答案 0 :(得分:1)

似乎翻转IsDirectionInversed属性并不会导致控件重新呈现,我说这是WPF中的错误(WPF中的错误!?没办法!)。

通过反复试验,我发现如果你使滑块的PART_Track部分无效(包含RepeatButtons和Thumb的元素),它会神奇地切换它,一切都按照应有的方式工作!

因此,如果您只是将Button_Click处理程序修改为这样,那么一切都应该正常工作。 : - )

private void Button_Click(object sender, RoutedEventArgs e)
{
    mSliderInverted = !mSliderInverted;
    OnPropertyChanged("Inverted");

    // Retrieve the Track from the Slider control
    var track = SliderControl.Template.FindName("PART_Track", SliderControl) as Track;

    // Force it to rerender
    track.InvalidateVisual();
}