当 IsDirectionReversed 更改时使 Thumb 位置无效
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();
}
我在 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();
}