2015-11-13 8 views
0

Есть ли кнопочный элемент управления в WPF/XAML, аналогичный экрану на Windows 10 (и Windows 8.1, я думаю)? Под этим я подразумеваю, что когда вы удерживаетесь, они «размахиваются» на несколько кнопок, которые можно перетащить и активировать при отпускании (даже если щелчок немного отходит от клавиши). Если нет, то как их реализовать?Кнопка вентилятора XAML

The picture is worth a thousand characters

ответ

2

Я не в курсе управления по умолчанию, чтобы сделать это. Тем не менее, я думаю, вы можете получить эту функциональность:

1) Наличие кнопки с основным выбором текста 2) Создание всплывающего окна с сеткой кнопок, содержащей значение по умолчанию, а также все варианты, которые вы хотите отобразить 3) Сохранение стиля основной кнопки и кнопок во всплывающем окне 4) Когда всплывающее окно отображается, поместите его так, чтобы основная текстовая кнопка совпадала с той, что показана во всплывающем окне.

Вот пример некоторых из этого. FanoutButton.xaml

<UserControl x:Class="FanoutButtonTest.FanoutButton" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      mc:Ignorable="d" 
      d:DesignHeight="64" d:DesignWidth="64" FontSize="20" FontWeight="Bold"> 
    <Grid> 
     <Button x:Name="btnMain" Background="#FFF6F6F6" Click="btnMain_Click"/> 
    </Grid> 
</UserControl> 

Код За:

public partial class FanoutButton : UserControl 
    { 
     private string _mainText; 
     private Popup _fanoutPopup; 

     public string MainText { get { return _mainText; } set { SetMainText(value); } } 

     public List<string> Variations { get; set; } 

     public delegate void ValueClickedHandler(string value); 
     public event ValueClickedHandler ValueClicked; 

     public FanoutButton() 
     { 
      InitializeComponent(); 
      InitializeVariables(); 
     } 

     private void InitializeVariables() 
     { 
      this.Variations = new List<string>(); 
     } 

     private void SetMainText(string value) 
     { 
      _mainText = value; 

      btnMain.Content = _mainText; 
     } 

     private void Hold() 
     { 
      //Calculate rows and columns for popup 
      int buttonCount = 1 + this.Variations.Count; 
      double squareRoot = Math.Sqrt((double)buttonCount); 
      int columns = (int)Math.Ceiling(squareRoot); 
      int rows = (int)Math.Round(squareRoot); 

      int width = (int)this.Width * columns; 
      int height = (int)this.Height * rows; 

      //Get button location 
      Point buttonPosition = btnMain.PointToScreen(new Point(0d, 0d)); 

      _fanoutPopup = new Popup(); 
      _fanoutPopup.Width = width; 
      _fanoutPopup.Height = height; 
      _fanoutPopup.HorizontalOffset = buttonPosition.X; 
      _fanoutPopup.VerticalOffset = buttonPosition.Y; 


      var allValues = new List<string>(); 
      allValues.Add(_mainText); 
      allValues.AddRange(this.Variations); 

      var container = new WrapPanel(); 
      _fanoutPopup.Child = container; 

      foreach (string value in allValues) 
      { 
       var button = new Button(); 
       button.Width = this.Width; 
       button.Height = this.Height; 
       button.Content = value; 
       button.Background = btnMain.Background; 
       button.Foreground = btnMain.Foreground; 
       button.Template = btnMain.Template; 
       button.Tag = value; 
       button.Click += button_Click; 

       container.Children.Add(button); 
      } 

      _fanoutPopup.IsOpen = true; 
     } 

     private void button_Click(object sender, RoutedEventArgs e) 
     { 
      string value = ""; 

      if (sender is Button) 
      { 
       value = ((Button)sender).Tag.ToString(); 

       _fanoutPopup.IsOpen = false; 

       RaiseValueClicked(value); 
      } 
     } 

     private void btnMain_Click(object sender, RoutedEventArgs e) 
     { 
      Hold(); 
     } 

     private void RaiseValueClicked(string value) 
     { 
      if (ValueClicked != null) 
      { 
       ValueClicked(value); 
      } 
     } 
} 

MainWindow.xaml:

<Window xmlns:FanoutButtonTest="clr-namespace:FanoutButtonTest" x:Class="FanoutButtonTest.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
     <FanoutButtonTest:FanoutButton x:Name="fbtnTest" Width="48" Height="48"/> 
    </Grid> 
</Window> 

MainWindow Код За:

общественного класса MainWindow парциальное: Окно { общественного MainWindow() { InitializeComponent();

fbtnTest.MainText = "a"; 
    fbtnTest.Variations = new List<string>() { "b", "c", "d", "e", "f", "g" }; 
    fbtnTest.ValueClicked += fbtnTest_ValueClicked; 
} 

private void fbtnTest_ValueClicked(string value) 
{ 
    MessageBox.Show("Clicked: " + value); 
} 

}

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