簡體   English   中英

IsDirectionReversed更改時,拇指位置無效

[英]Invalidate Thumb position when IsDirectionReversed changes

我在C#WPF應用程序中使用滑塊控件,並希望為用戶提供即時反轉控件的功能。

我已經確定滑塊控件具有內置的IsDirectionReversed屬性,該屬性很好地用於更改控件的方向(有效地將其最小值和最大值交換到控件的相對兩端)。 但是,在即時更改屬性時,拇指的位置會保持在原位置(在視覺上暗示不正確的值),但其值會保持不變(與我期望的一樣)。

必要行為的例子

(在反轉控件后,單擊軌道上的任何位置都會使拇指更新為當前值的+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;
}

似乎翻轉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();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM