Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# XAML扇出按钮_C#_Wpf_Xaml - Fatal编程技术网

C# XAML扇出按钮

C# XAML扇出按钮,c#,wpf,xaml,C#,Wpf,Xaml,WPF/XAML中是否有类似于Windows10(我想还有Windows8.1)屏幕键盘中的按钮控件?我的意思是当按下时,它们“扇出”成多个按钮,释放时可以拖动到并激活这些按钮(即使单击稍微远离按键)。若否,如何落实? 我不知道有默认控件可以执行此操作。但是,我认为您可以通过以下方式获得此功能: 1) 有一个包含主文本选择的按钮 2) 创建一个包含默认值和所有要显示的变体的按钮网格弹出窗口 3) 保持主按钮和弹出窗口中按钮的样式一致 4) 显示弹出窗口时,将其定位,使主文本按钮与弹出窗口中显示的

WPF/XAML中是否有类似于Windows10(我想还有Windows8.1)屏幕键盘中的按钮控件?我的意思是当按下时,它们“扇出”成多个按钮,释放时可以拖动到并激活这些按钮(即使单击稍微远离按键)。若否,如何落实?


我不知道有默认控件可以执行此操作。但是,我认为您可以通过以下方式获得此功能:

1) 有一个包含主文本选择的按钮 2) 创建一个包含默认值和所有要显示的变体的按钮网格弹出窗口 3) 保持主按钮和弹出窗口中按钮的样式一致 4) 显示弹出窗口时,将其定位,使主文本按钮与弹出窗口中显示的按钮对齐

这里有一个这样的例子。 范伯顿

<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);
            }
        }
}
公共部分类FanBurtton:UserControl
{
私有字符串_mainText;
专用弹出窗口_fanoupopup;
公共字符串MainText{get{return}MainText;}set{SetMainText(value);}}
公共列表变量{get;set;}
公共委托无效值ClickedHandler(字符串值);
公共事件值ClickedHandler值Clicked;
公众球迷()
{
初始化组件();
初始化变量();
}
私有无效初始化变量()
{
this.Variations=新列表();
}
私有void SetMainText(字符串值)
{
_mainText=值;
btnMain.Content=\u mainText;
}
私人无效持有
{
//计算弹出窗口的行和列
int buttonCount=1+this.Variations.Count;
双平方根=数学Sqrt((双)按钮计数);
整数列=(整数)数学上限(平方根);
int行=(int)数学四舍五入(平方根);
int width=(int)this.width*列;
int height=(int)this.height*行;
//获取按钮位置
点按钮位置=BTN主点到屏幕(新点(0d,0d));
_FanOutpupp=新弹出窗口();
_风扇输出端口宽度=宽度;
_风扇输出功率高度=高度;
_fanoutPopup.HorizontalOffset=按钮位置.X;
_FANOTPOPUP.VerticalOffset=按钮位置Y;
var allValues=新列表();
添加(_mainText);
allValues.AddRange(此.Variations);
var container=newwrappanel();
_fanoutPopup.Child=容器;
foreach(所有值中的字符串值)
{
var按钮=新按钮();
button.Width=此.Width;
button.Height=此.Height;
按钮。内容=值;
button.Background=btnMain.Background;
button.Foreground=btnMain.Foreground;
button.Template=btnMain.Template;
标签=值;
按钮。单击+=按钮\u单击;
container.Children.Add(按钮);
}
_fanouppopp.IsOpen=真;
}
私有无效按钮\u单击(对象发送者,路由目标e)
{
字符串值=”;
如果(发送者是按钮)
{
value=((按钮)sender.Tag.ToString();
_fanoutPopup.IsOpen=假;
RaiseValueClicked(值);
}
}
私有无效BTN主单击(对象发送者,路由目标e)
{
Hold();
}
私有void RaiseValueClicked(字符串值)
{
如果(ValueClicked!=null)
{
值(值);
}
}
}
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>

主窗口代码隐藏:

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);
            }
        }
}
公共部分类主窗口:窗口 { 公共主窗口() { 初始化组件()

fbttest.MainText=“a”;
Variations=newlist(){“b”、“c”、“d”、“e”、“f”、“g”};
fbtnTest.ValueClicked+=fbtnTest\u ValueClicked;
}
私有void fbtnTest_值已单击(字符串值)
{
MessageBox.Show(“单击:+值);
}
}