从WPF中的Behind代码将Shapes转换为按钮 [英] Converting Shapes in to button from code Behind in WPF

查看:61
本文介绍了从WPF中的Behind代码将Shapes转换为按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

在WPf中,我们可以通过Xaml将形状(矩形,椭圆形等)转换为按钮,即在设计模式下.
类似地,我们可以从后面的代码中将形状(矩形或椭圆形)转换为按钮.
即,我们可以在运行时通过拖动鼠标在WPF窗口上绘制形状.现在,我们可以将这种形状转换为按钮,并在单击特定形状时显示一些消息.

请举个例子,我们如何做到这一点.任何帮助都将不胜感激.

谢谢,

Hello,

In WPf we can convert shapes(rectangle, ellipse etc) in to a button through Xaml i.e., in design mode.
similarly can we convert a shape(rectangle or ellipse) in to a button from code behind.
ie., we can draw shapes at run time on the WPF window by dragging Mouse. Now can we convert this shapes in to button and display some message when we click on the particular shape.

Plz give any example how can we do that. any help acn be appreciated.

Thanks,

推荐答案

好.这很容易.我给你举个例子.在此示例中,您将看到我使用了标签和按钮.当我们单击按钮时,标签将增大/缩小.但是我已经从后面的代码中维护了它

xaml在下面给出

Well. This is pretty easy. I am giving you a example. In this example you will see that I have used label and a button. When we click the button the label will Grow/Shrink. But i have maintain it from code behind

The xaml is given below

<Window x:Class="WpfTest.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 Height="311" Width="474">
        <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="188,263,0,0" Name="button1" VerticalAlignment="Top" Width="100" Click="button1_Click" />
        <Label Content="Hi!!" Height="100" HorizontalAlignment="Center" Margin="188,76,186,0" Name="label1" VerticalAlignment="Top" Width="100" Background="#FFBBF23E"  />
    </Grid>
</Window>



后面的代码在下面给出



And the code behind is given below

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Button btn = (Button)sender;
            Grid grd = btn.Parent as Grid;

            Label lbl = grd.Children[1] as Label;

            if (lbl.Width < 100)
            {
                lbl.Width = 100;
            }
            else
            {
                lbl.Width = 50;
            }

            if (lbl.Height < 100)
            {
                lbl.Height = 100;
            }
            else
            {
                lbl.Height = 50;
            }
        }
    }
}



因此,我在这里所做的是,我只获取了按钮对象,然后是网格的按钮的父对象.在此网格中,第二个孩子是我的标签,一旦有了标签对象,就可以像在xaml中一样更改其属性.



So What i am doing here is that I just get the Button Object and then the Parent of the Button Which is Grid. In this Grid the Second Child is My Label and Once we have the Label Object we could change its Property as we could do in the xaml.


MVKbgl,谢谢您的提问.我已更新我的答案.

要制作按钮或任何其他控件,您可能需要继承该控件,然后可以添加您的自定义属性.在这里,我创建了一个名为EclipseButton的自定义按钮,该按钮通常是一个用户控件.请看一下控件上的XAMl

MVKbgl, Thanks for your Question. I have Updated My Answer.

To make a button or any other control you might need to inherit the Control and then could add your custom property. Here I have created a custom button named EclipseButton which is usually a usercontrol. Please have a look on the XAMl on the control

<UserControl x:Class="WpfTest.EclipseButton"

             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"

             >
    <Grid>
        <Button>
            <StackPanel Orientation="Horizontal" Margin="10">
                <Ellipse  Name="ellipse1" Stroke="Black"

                      Width="{Binding ElementName=UC, Path=ButtonWidth}"

                       Height="{Binding ElementName=UC, Path=ButtonHeight}"

                          />
                <TextBlock Text="{Binding ElementName=UC, Path=Text}"

                           Margin="10,0,0,0"/>
            </StackPanel>
        </Button>

    </Grid>
</UserControl>



这个控件的代码如下所示,



And the code behind of this control looks like that,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfTest
{
    /// <summary>
    /// Interaction logic for EclipseButton.xaml
    /// </summary>
    public partial class EclipseButton : UserControl
    {
        public EclipseButton()
        {
            InitializeComponent();
        }

        public double ButtonWidth
        {
            get { return (double)GetValue(WidthProperty); }
            set { SetValue(WidthProperty, value); }
        }

        public static readonly DependencyProperty WidthProperty =
            DependencyProperty.Register("ImageWidth", typeof(double), typeof(EclipseButton), new UIPropertyMetadata(16d));

        public double ButtonHeight
        {
            get { return (double)GetValue(HeightProperty); }
            set { SetValue(HeightProperty, value); }
        }

        public static readonly DependencyProperty HeightProperty =
            DependencyProperty.Register("ImageHeight", typeof(double), typeof(EclipseButton), new UIPropertyMetadata(16d));

        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof(string), typeof(EclipseButton), new UIPropertyMetadata(""));
    }
}




现在主窗口的XAML是,




And now the XAML of main window is ,

<Window x:Class="WpfTest.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"

        xmlns:my="clr-namespace:WpfTest">
    <Grid Height="311" Width="474">
        <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="188,263,0,0" Name="button1" VerticalAlignment="Top" Width="100" Click="button1_Click" />
        <Label Content="Hi!!" Height="100" HorizontalAlignment="Center" Margin="188,76,186,0" Name="label1" VerticalAlignment="Top" Width="100" Background="#FFBBF23E"  />

        <my:EclipseButton Text="Eclipse Button" HorizontalAlignment="Left" VerticalAlignment="Top"

                        ButtonWidth="16" ButtonHeight="16" Margin="10" ButtonBase.Click="Button_Click" />
    </Grid>
</Window>




这个主窗口后面的代码是




And the Code behind of this main window is

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Button btn = (Button)sender;
            Grid grd = btn.Parent as Grid;

            Label lbl = grd.Children[1] as Label;

            if (lbl.Width < 100)
            {
                lbl.Width = 100;
            }
            else
            {
                lbl.Width = 50;
            }

            if (lbl.Height < 100)
            {
                lbl.Height = 100;
            }
            else
            {
                lbl.Height = 50;
            }
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            EclipseButton btn = (EclipseButton)sender;
            Grid grd = btn.Parent as Grid;

            Label lbl = grd.Children[1] as Label;

            if (lbl.Width < 100)
            {
                lbl.Width = 100;
            }
            else
            {
                lbl.Width = 50;
            }

            if (lbl.Height < 100)
            {
                lbl.Height = 100;
            }
            else
            {
                lbl.Height = 50;
            }
        }
    }
    
}



在此代码中,您将看到我使用了自己的按钮,该按钮具有Click事件,并且该事件在主窗口后面的代码中进行处理.

我认为它将为您提供足够的帮助.如果您需要任何帮助,请告诉我.

谢谢,
Rashim



In this code you will see that i have used my own button which has a Click Event and this event is handle in the code behind of main window.

I think it will help you enough. And let me know if you need any assistance please.

Thanks,
Rashim


我不确定您是否真的需要这样做. Shapes已经支持键盘和鼠标事件并获得焦点.只需将事件处理程序挂接到它上,它将像按钮一样起作用.
I''m not sure you actually need to do this. Shapes already support keyboard and mouse events and receiving focus. Just hook event handlers up to it and it will act just like a button.


这篇关于从WPF中的Behind代码将Shapes转换为按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆