2016-04-17 2 views
-1

enter image description here enter image description hereWie eine Liste von Checkboxen wie im Alarm & Clock App

Wie kann ich solche alist von Kontrollkästchen machen machen, dass sie auf die Schaltfläche ausgerichtet sind links, von wo aus sie sich öffnet, und ein zur Verfügung stellen Öffnen und Schließen von Animationen, wie in der Wecker-App & in Windows 10? Ich habe versucht mit Popups und Flyouts, aber es gibt nicht den gleichen Effekt.

Antwort

1

Es sieht aus, dass diese Steuerung im Alarm & Uhr App tatsächlich von einer ComboBox Steuerung erweitert wird.

So können wir eine ComboBox, dies zu tun, zum Beispiel:

<ComboBox x:Name="comboBox" VerticalAlignment="Center" HorizontalAlignment="Stretch" ItemsSource="{x:Bind comboitems}" 
      PlaceholderText="{x:Bind contenttext, Mode=OneWay}" DropDownClosed="comboBox_DropDownClosed" SelectionChanged="comboBox_SelectionChanged"> 
    <ComboBox.ItemTemplate> 
     <DataTemplate> 
      <CheckBox Content="{Binding dayofweek}" IsChecked="{Binding ischecked, Mode=TwoWay}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" /> 
     </DataTemplate> 
    </ComboBox.ItemTemplate> 
</ComboBox> 

die ausgewählten Tage Um zu zeigen, auf die ComboBox ein wenig komplex ist, können wir diese Arbeit in dem hinter Code tun:

public sealed partial class MainPage : Page, INotifyPropertyChanged 
{ 
    private ObservableCollection<comboItem> comboitems = new ObservableCollection<comboItem>(); 

    private string _contenttext; 

    public string contenttext 
    { 
     get 
     { 
      return _contenttext; 
     } 
     set 
     { 
      if (value != _contenttext) 
      { 
       _contenttext = value; 
       OnPropertyChanged(); 
      } 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

    private void OnPropertyChanged([CallerMemberName] string propertyName = "") 
    { 
     if (this.PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 

    public MainPage() 
    { 
     this.InitializeComponent(); 
     this.Loaded += MainPage_Loaded; 
    } 

    private int daycount; 

    private void MainPage_Loaded(object sender, RoutedEventArgs e) 
    { 
     showdays(); 
    } 

    protected override void OnNavigatedTo(NavigationEventArgs e) 
    { 
     comboitems.Clear(); 
     comboitems.Add(new comboItem { dayofweek = "Sunday", ischecked = false }); 
     comboitems.Add(new comboItem { dayofweek = "Monday", ischecked = true }); 
     comboitems.Add(new comboItem { dayofweek = "Tuesday", ischecked = true }); 
     comboitems.Add(new comboItem { dayofweek = "Wednesday", ischecked = true }); 
     comboitems.Add(new comboItem { dayofweek = "Thursday", ischecked = true }); 
     comboitems.Add(new comboItem { dayofweek = "Friday", ischecked = true }); 
     comboitems.Add(new comboItem { dayofweek = "Saturday", ischecked = false }); 
    } 

    private void comboBox_DropDownClosed(object sender, object e) 
    { 
     showdays(); 
    } 

    private void comboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
     comboBox.SelectedIndex = -1; 
    } 

    private void showdays() 
    { 
     contenttext = null; 
     daycount = 0; 
     for (int i = 0; i < comboBox.Items.Count(); i++) 
     { 
      comboItem item = comboBox.Items.ElementAt(i) as comboItem; 
      if (item.ischecked) 
      { 
       contenttext = contenttext + item.dayofweek.Substring(0, 3) + ", "; 
       daycount++; 
      } 
     } 

     if (daycount != 0) 
     { 
      if (daycount == 2 && contenttext == "Sun, Sat, ") 
      { 
       contenttext = "Weekends"; 
      } 
      else if (daycount == 5 && contenttext == "Mon, Tue, Wed, Thu, Fri, ") 
      { 
       contenttext = "Weekdays"; 
      } 
      else if (daycount == 7) 
      { 
       contenttext = "Every day"; 
      } 
      else 
      { 
       contenttext = contenttext.TrimEnd(' ', ','); 
      } 
     } 
     else 
     { 
      contenttext = "Only once"; 
     } 
    } 

} 

Und die comboItem Klasse an meiner Seite ist wie folgt:

public class comboItem : INotifyPropertyChanged 
{ 
    public string dayofweek { get; set; } 

    private bool _ischecked; 

    public bool ischecked 
    { 
     get 
     { 
      return _ischecked; 
     } 
     set 
     { 
      if (value != _ischecked) 
      { 
       _ischecked = value; 
       OnPropertyChanged(); 
      } 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

    private void OnPropertyChanged([CallerMemberName] string propertyName = "") 
    { 
     if (this.PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 
} 

Vielleicht finden Sie eine andere einfachere Möglichkeit, Tage in bestimmte Zeichenfolge zu konvertieren, schließlich funktioniert meine Methode hier.

aktualisieren:

Hier ist die gesamte geänderte Vorlage Stil des ComboBox, können Sie auch die Vorlage von Ihrem Selbst bearbeiten.

<Style TargetType="ComboBox"> 
    <Setter Property="Padding" Value="12,5,0,7" /> 
    <Setter Property="MinWidth" Value="{ThemeResource ComboBoxThemeMinWidth}" /> 
    <Setter Property="Foreground" Value="{ThemeResource SystemControlForegroundBaseHighBrush}" /> 
    <Setter Property="Background" Value="{ThemeResource SystemControlBackgroundAltMediumLowBrush}" /> 
    <Setter Property="BorderBrush" Value="{ThemeResource SystemControlForegroundBaseMediumLowBrush}" /> 
    <Setter Property="BorderThickness" Value="0" /> 
    <Setter Property="TabNavigation" Value="Once" /> 
    <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled" /> 
    <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" /> 
    <Setter Property="ScrollViewer.HorizontalScrollMode" Value="Disabled" /> 
    <Setter Property="ScrollViewer.VerticalScrollMode" Value="Auto" /> 
    <Setter Property="ScrollViewer.IsVerticalRailEnabled" Value="True" /> 
    <Setter Property="ScrollViewer.IsDeferredScrollingEnabled" Value="False" /> 
    <Setter Property="ScrollViewer.BringIntoViewOnFocusChange" Value="True" /> 
    <Setter Property="HorizontalContentAlignment" Value="Stretch" /> 
    <Setter Property="HorizontalAlignment" Value="Left" /> 
    <Setter Property="VerticalAlignment" Value="Top" /> 
    <Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}" /> 
    <Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}" /> 
    <Setter Property="ItemsPanel"> 
     <Setter.Value> 
      <ItemsPanelTemplate> 
       <CarouselPanel /> 
      </ItemsPanelTemplate> 
     </Setter.Value> 
    </Setter> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="ComboBox"> 
       <Grid> 
        <Grid.ColumnDefinitions> 
         <ColumnDefinition Width="*" /> 
         <ColumnDefinition Width="32" /> 
        </Grid.ColumnDefinitions> 
        <Grid.RowDefinitions> 
         <RowDefinition Height="Auto" /> 
         <RowDefinition Height="*" /> 
        </Grid.RowDefinitions> 
        <VisualStateManager.VisualStateGroups> 
         <VisualStateGroup x:Name="CommonStates"> 
          <VisualState x:Name="Normal" /> 
          <VisualState x:Name="PointerOver"> 
           <Storyboard> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="Background"> 
             <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlPageBackgroundAltMediumBrush}" /> 
            </ObjectAnimationUsingKeyFrames> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="Background"> 
             <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightBaseMediumBrush}" /> 
            </ObjectAnimationUsingKeyFrames> 
            <!--<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="PlaceholderTextBlock"> 
             <DiscreteObjectKeyFrame KeyTime="0" Value="LightBlue" /> 
            </ObjectAnimationUsingKeyFrames>--> 
           </Storyboard> 
          </VisualState> 
          <VisualState x:Name="Pressed"> 
           <Storyboard> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="Background"> 
             <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlBackgroundListMediumBrush}" /> 
            </ObjectAnimationUsingKeyFrames> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="Background"> 
             <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightBaseMediumLowBrush}" /> 
            </ObjectAnimationUsingKeyFrames> 
           </Storyboard> 
          </VisualState> 
          <VisualState x:Name="Disabled"> 
           <Storyboard> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="Background"> 
             <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlBackgroundBaseLowBrush}" /> 
            </ObjectAnimationUsingKeyFrames> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="Background"> 
             <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlDisabledBaseLowBrush}" /> 
            </ObjectAnimationUsingKeyFrames> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="HeaderContentPresenter"> 
             <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlDisabledBaseMediumLowBrush}" /> 
            </ObjectAnimationUsingKeyFrames> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter"> 
             <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlDisabledBaseMediumLowBrush}" /> 
            </ObjectAnimationUsingKeyFrames> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="PlaceholderTextBlock"> 
             <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlDisabledBaseMediumLowBrush}" /> 
            </ObjectAnimationUsingKeyFrames> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="DropDownGlyph"> 
             <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlDisabledBaseMediumLowBrush}" /> 
            </ObjectAnimationUsingKeyFrames> 
           </Storyboard> 
          </VisualState> 
         </VisualStateGroup> 
         <VisualStateGroup x:Name="FocusStates"> 
          <VisualState x:Name="Focused"> 
           <Storyboard> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="HighlightBackground"> 
             <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightTransparentBrush}" /> 
            </ObjectAnimationUsingKeyFrames> 
            <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="HighlightBackground" /> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter"> 
             <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAltBaseHighBrush}" /> 
            </ObjectAnimationUsingKeyFrames> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="PlaceholderTextBlock"> 
             <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAltBaseHighBrush}" /> 
            </ObjectAnimationUsingKeyFrames> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="DropDownGlyph"> 
             <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAltBaseMediumHighBrush}" /> 
            </ObjectAnimationUsingKeyFrames> 
           </Storyboard> 
          </VisualState> 
          <VisualState x:Name="FocusedPressed"> 
           <Storyboard> 
            <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="HighlightBackground" /> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter"> 
             <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAltBaseHighBrush}" /> 
            </ObjectAnimationUsingKeyFrames> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="PlaceholderTextBlock"> 
             <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAltBaseHighBrush}" /> 
            </ObjectAnimationUsingKeyFrames> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="DropDownGlyph"> 
             <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAltBaseMediumHighBrush}" /> 
            </ObjectAnimationUsingKeyFrames> 
           </Storyboard> 
          </VisualState> 
          <VisualState x:Name="Unfocused" /> 
          <VisualState x:Name="PointerFocused" /> 
          <VisualState x:Name="FocusedDropDown"> 
           <Storyboard> 
            <ObjectAnimationUsingKeyFrames Duration="0" Storyboard.TargetProperty="Visibility" Storyboard.TargetName="PopupBorder"> 
             <DiscreteObjectKeyFrame KeyTime="0"> 
              <DiscreteObjectKeyFrame.Value> 
               <Visibility>Visible</Visibility> 
              </DiscreteObjectKeyFrame.Value> 
             </DiscreteObjectKeyFrame> 
            </ObjectAnimationUsingKeyFrames> 
           </Storyboard> 
          </VisualState> 
         </VisualStateGroup> 
         <VisualStateGroup x:Name="DropDownStates"> 
          <VisualState x:Name="Opened"> 
           <Storyboard> 
            <SplitOpenThemeAnimation ClosedTargetName="ContentPresenter" OffsetFromCenter="{Binding TemplateSettings.DropDownOffset, RelativeSource={RelativeSource Mode=TemplatedParent}}" OpenedTargetName="PopupBorder" OpenedLength="{Binding TemplateSettings.DropDownOpenedHeight, RelativeSource={RelativeSource Mode=TemplatedParent}}" /> 
           </Storyboard> 
          </VisualState> 
          <VisualState x:Name="Closed"> 
           <Storyboard> 
            <SplitCloseThemeAnimation ClosedTargetName="ContentPresenter" OffsetFromCenter="{Binding TemplateSettings.DropDownOffset, RelativeSource={RelativeSource Mode=TemplatedParent}}" OpenedTargetName="PopupBorder" OpenedLength="{Binding TemplateSettings.DropDownOpenedHeight, RelativeSource={RelativeSource Mode=TemplatedParent}}" /> 
           </Storyboard> 
          </VisualState> 
         </VisualStateGroup> 
        </VisualStateManager.VisualStateGroups> 
        <ContentPresenter x:Name="HeaderContentPresenter" ContentTemplate="{TemplateBinding HeaderTemplate}" Content="{TemplateBinding Header}" FontWeight="{ThemeResource ComboBoxHeaderThemeFontWeight}" FlowDirection="{TemplateBinding FlowDirection}" Margin="{ThemeResource ComboBoxHeaderThemeMargin}" Visibility="Collapsed" x:DeferLoadStrategy="Lazy" /> 
        <Border x:Name="Background" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Grid.ColumnSpan="2" Grid.Row="1" /> 
        <Border x:Name="HighlightBackground" BorderBrush="{ThemeResource SystemControlHighlightBaseMediumLowBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{ThemeResource SystemControlHighlightListAccentLowBrush}" Grid.ColumnSpan="2" Opacity="0" Grid.Row="1" /> 
        <ContentPresenter x:Name="ContentPresenter" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" Grid.Row="1" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Grid.ColumnSpan="2"> 
         <TextBlock x:Name="PlaceholderTextBlock" Foreground="{ThemeResource SystemControlForegroundAccentBrush}" Text="{TemplateBinding PlaceholderText}" /> 
        </ContentPresenter> 
        <!--<FontIcon x:Name="DropDownGlyph" AutomationProperties.AccessibilityView="Raw" Grid.Column="1" Foreground="{ThemeResource SystemControlForegroundBaseMediumHighBrush}" FontSize="12" FontFamily="{ThemeResource SymbolThemeFontFamily}" Glyph="&#xE0E5;" HorizontalAlignment="Right" IsHitTestVisible="False" Margin="0,10,10,10" Grid.Row="1" VerticalAlignment="Center" />--> 
        <Popup x:Name="Popup"> 
         <Border x:Name="PopupBorder" BorderBrush="{ThemeResource SystemControlForegroundChromeHighBrush}" BorderThickness="{ThemeResource ComboBoxDropdownBorderThickness}" Background="{ThemeResource SystemControlBackgroundChromeMediumLowBrush}" HorizontalAlignment="Stretch" Margin="0,-1,0,-1"> 
          <ScrollViewer x:Name="ScrollViewer" AutomationProperties.AccessibilityView="Raw" BringIntoViewOnFocusChange="{TemplateBinding ScrollViewer.BringIntoViewOnFocusChange}" Foreground="{ThemeResource SystemControlForegroundBaseHighBrush}" HorizontalScrollMode="{TemplateBinding ScrollViewer.HorizontalScrollMode}" HorizontalScrollBarVisibility="{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}" IsHorizontalRailEnabled="{TemplateBinding ScrollViewer.IsHorizontalRailEnabled}" IsVerticalRailEnabled="{TemplateBinding ScrollViewer.IsVerticalRailEnabled}" IsDeferredScrollingEnabled="{TemplateBinding ScrollViewer.IsDeferredScrollingEnabled}" MinWidth="{Binding TemplateSettings.DropDownContentMinWidth, RelativeSource={RelativeSource Mode=TemplatedParent}}" VerticalSnapPointsType="OptionalSingle" VerticalScrollBarVisibility="{TemplateBinding ScrollViewer.VerticalScrollBarVisibility}" VerticalScrollMode="{TemplateBinding ScrollViewer.VerticalScrollMode}" VerticalSnapPointsAlignment="Near" ZoomMode="Disabled"> 
           <ItemsPresenter Margin="{ThemeResource ComboBoxDropdownContentMargin}" /> 
          </ScrollViewer> 
         </Border> 
        </Popup> 
       </Grid> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

entfernen Es ist nur die FontIcon innerhalb dieser Steuerung und machen die TextBlock ‚s Column = 2 und SystemControlForegroundAccentBrush Vordergrund es ändern. Wenn Sie es von Ihrem Selbst ändern möchten, können Sie das Dokument OutLine öffnen, um dieses ComboBox finden, direkt darauf klicken, dann wählen Sie Bearbeiten Vorlage dann eine Kopie bearbeiten, so dass Sie die Standardvorlage von ComboBox erhalten:

enter image description here

+0

Ich werde das versuchen, danke. Aber wie haben sie die ComboBox mit dem anklickbaren TexTButton benutzt? – andy

+0

@andy, mit klickbarem TextButton, meinen Sie das Element der ComboBox? –

+0

Ich habe zur Verdeutlichung ein weiteres Bild hinzugefügt und den anklickbaren TextBlock markiert. – andy