使滑块值仅从绑定值更改

时间:2013-05-12 11:05:50

标签: windows-runtime winrt-xaml

我试图使滑块值仅从数据绑定中设置。我不希望用户能够手动更改滑块值。这是片段。关于如何做的任何建议。

<Slider  x:Name="Serverslider"  Value="{Binding Value}"   

                               />

我遗失的任何属性?

1 个答案:

答案 0 :(得分:1)

正如wojtek建议的那样,你可以使用IsEnabled =“False”来禁止输入影响Slider。你明白了:

现在,您还需要更改Slider的填充颜色。这里有两个选项:

  1. 重新尝试滑块并更改“已禁用”的可视状态以查看您的需求。在“混合”中,右键单击“滑块”,然后选择“编辑模板&gt;编辑副本”。然后,找到您想要更改的颜色。在这里,这些将是填充颜色,Thumb Background和Thumb BorderBrush for Disabled状态。例如,更改以下XAML:

    <VisualState x:Name="Disabled">
      <Storyboard>
        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Fill" Storyboard.TargetName="HorizontalDecreaseRect">
          <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource SliderTrackDecreaseDisabledBackgroundThemeBrush}"/>
        </ObjectAnimationUsingKeyFrames>
        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="HorizontalThumb">
          <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource SliderThumbDisabledBackgroundThemeBrush}"/>
        </ObjectAnimationUsingKeyFrames>
        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="HorizontalThumb">
          <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource SliderThumbDisabledBackgroundThemeBrush}"/>
        </ObjectAnimationUsingKeyFrames>
    

    到此:

    <VisualState x:Name="Disabled">
      <Storyboard>
        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Fill" Storyboard.TargetName="HorizontalDecreaseRect">
          <DiscreteObjectKeyFrame KeyTime="0" Value="Red"/>
        </ObjectAnimationUsingKeyFrames>
        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="HorizontalThumb">
          <DiscreteObjectKeyFrame KeyTime="0" Value="Orange"/>
        </ObjectAnimationUsingKeyFrames>
        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="HorizontalThumb">
          <DiscreteObjectKeyFrame KeyTime="0" Value="Yellow"/>
        </ObjectAnimationUsingKeyFrames>
    

    注意:如果您关心垂直滑块模板,请编辑标有“垂直*”的零件。 示例代码:https://github.com/finnigantime/Samples/tree/master/examples/Win8Xaml/Slider_RetemplateDisabledState

  2. 如果您应用中的所有已禁用滑块看起来都相同,则可以覆盖用于在app.xaml中绘制已禁用滑块的“主题资源”:

    <Application>
      <Application.Resources>
        <ResourceDictionary>
          <SolidColorBrush x:Key="SliderTrackDecreaseDisabledBackgroundThemeBrush" Color="Green" />
          <SolidColorBrush x:Key="SliderThumbDisabledBackgroundThemeBrush" Color="Lime" />
        </ResourceDictionary>
    

    示例代码:https://github.com/finnigantime/Samples/tree/master/examples/Win8Xaml/Slider_OverrideDisabledResources