0

У меня есть некоторые Button в моем MainPage, как показано ниже:UWP XAML VisualState не доступный сеттер

<Grid Grid.Row="1"> 
     <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center" Margin="10" > 
      <Image Stretch="None" Source="Images/GameMainMenuIcon.png" Margin="0 0 0 50" /> 
      <Button Style="{StaticResource ButtonGameLarge}">Button1</Button> 
      <Button Style="{StaticResource ButtonGameLarge}">Button2</Button> 
      <Button Style="{StaticResource ButtonGameLarge}">Button3</Button> 
     </StackPanel> 
</Grid> 

И в моем Style.xaml файле я написал эти стили для Buttons:

<Style TargetType="Button" x:Key="ButtonGameLarge"> 
    <Setter Property="VisualStateManager.VisualStateGroups"> 
     <Setter.Value> 
      <VisualStateGroup> 

       <VisualState> 
        <VisualState.StateTriggers> 
         <AdaptiveTrigger MinWindowWidth="720" /> 
        </VisualState.StateTriggers> 
        <VisualState.Setters> 
         <Setter Property="Height" Value="80" /> 
         <Setter Property="Width" Value="400" /> 
         <Setter Property="Margin" Value="0 0 0 20" /> 
        </VisualState.Setters> 
       </VisualState> 

       <VisualState> 
        <VisualState.StateTriggers> 
         <AdaptiveTrigger MinWindowWidth="400" /> 
        </VisualState.StateTriggers> 
        <VisualState.Setters> 
         <Setter Property="Height" Value="40" /> 
         <Setter Property="Width" Value="200" /> 
         <Setter Property="Margin" Value="0 0 0 10" /> 
        </VisualState.Setters> 
       </VisualState> 

      </VisualStateGroup> 
     </Setter.Value> 
    </Setter> 

</Style> 

Но это дает мне следующие ошибки:

  • Недвижимость "Visua lStateGroups "не имеет доступного сеттера.
  • Невозможно назначить «VisualStateGroup» в свойство «VisualStateGroups», тип должен быть назначен для «IList»
  • Свойство «VisualStateGroups» не является DependencyProperty. Для использования в разметке не привязанные свойства должны отображаться в целевом типе с доступным свойством экземпляра «VisualStateGroups». Для прикрепленных свойств тип объявления должен предоставлять статические методы «GetVisualStateGroups» и «SetVisualStateGroups».

Как установить несколько VisualState для группы элементов?

ответ

0

Прилагаемое свойство VisualStateManager.VisualStateGroups может применяться только к типам, которые производятся от FrameworkElement.

Для вашей проблемы, вы можете изменить стиль, как это:

<Style x:Key="ButtonGameLarge" TargetType="Button"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="Button"> 
       <Grid x:Name="RootGrid" Background="{TemplateBinding Background}"> 
        <VisualStateManager.VisualStateGroups> 
         <VisualStateGroup x:Name="CommonStates"> 
          <VisualState x:Name="Normal"> 
           <Storyboard> 
            <PointerUpThemeAnimation Storyboard.TargetName="RootGrid" /> 
           </Storyboard> 
          </VisualState> 
          <VisualState x:Name="PointerOver"> 
           <Storyboard> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ContentPresenter"> 
             <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightBaseMediumLowBrush}" /> 
            </ObjectAnimationUsingKeyFrames> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter"> 
             <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightBaseHighBrush}" /> 
            </ObjectAnimationUsingKeyFrames> 
            <PointerUpThemeAnimation Storyboard.TargetName="RootGrid" /> 
           </Storyboard> 
          </VisualState> 
          <VisualState x:Name="Pressed"> 
           <Storyboard> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="RootGrid"> 
             <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlBackgroundBaseMediumLowBrush}" /> 
            </ObjectAnimationUsingKeyFrames> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ContentPresenter"> 
             <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightTransparentBrush}" /> 
            </ObjectAnimationUsingKeyFrames> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter"> 
             <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightBaseHighBrush}" /> 
            </ObjectAnimationUsingKeyFrames> 
            <PointerDownThemeAnimation Storyboard.TargetName="RootGrid" /> 
           </Storyboard> 
          </VisualState> 
          <VisualState x:Name="Disabled"> 
           <Storyboard> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="RootGrid"> 
             <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlBackgroundBaseLowBrush}" /> 
            </ObjectAnimationUsingKeyFrames> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter"> 
             <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlDisabledBaseMediumLowBrush}" /> 
            </ObjectAnimationUsingKeyFrames> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ContentPresenter"> 
             <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlDisabledTransparentBrush}" /> 
            </ObjectAnimationUsingKeyFrames> 
           </Storyboard> 
          </VisualState> 
         </VisualStateGroup> 
         <VisualStateGroup> 
          <VisualState> 
           <VisualState.StateTriggers> 
            <AdaptiveTrigger MinWindowWidth="720" /> 
           </VisualState.StateTriggers> 
           <VisualState.Setters> 
            <Setter Target="RootGrid.Height" Value="80" /> 
            <Setter Target="RootGrid.Width" Value="400" /> 
            <Setter Target="RootGrid.Margin" Value="0,0,0,20" /> 
           </VisualState.Setters> 
          </VisualState> 

          <VisualState> 
           <VisualState.StateTriggers> 
            <AdaptiveTrigger MinWindowWidth="400" /> 
           </VisualState.StateTriggers> 
           <VisualState.Setters> 
            <Setter Target="RootGrid.Height" Value="40" /> 
            <Setter Target="RootGrid.Width" Value="200" /> 
            <Setter Target="RootGrid.Margin" Value="0,0,0,10" /> 
           </VisualState.Setters> 
          </VisualState> 
         </VisualStateGroup> 
        </VisualStateManager.VisualStateGroups> 
        <ContentPresenter x:Name="ContentPresenter" AutomationProperties.AccessibilityView="Raw" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" ContentTemplate="{TemplateBinding ContentTemplate}" ContentTransitions="{TemplateBinding ContentTransitions}" Content="{TemplateBinding Content}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Padding="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" /> 
       </Grid> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

, как это в моем коде, мы можем прикрепить VisualStateManager.VisualStateGroups к RootGridButton внутри которой полученный из FrameworkElement.

0

Вы должны установить визуальные состояния в шаблоне управления, а затем повторно использовать элемент управления несколько раз. Здесь вы можете найти examples

Смежные вопросы