微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – WPF边框形状

我想创建一个类似Lync 2013的弹出窗口:

我感兴趣的是用这个小插图形状创建一个控件.

我尝试在内部创建一个带有Canvas的UserControl,并创建一个带有形状的Path.但是我没有发现Canvas非常友好,所以我想知道我是否可以通过“边框”控件“玩”来实现这一点,以便只放置一个边框,然后放入一个网格.

这可能吗?有人可以帮助我走上正轨吗?

解决方法

这是我的XAML:
<Window x:Class="CustomBorderStyle.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        LocationChanged="Window_LocationChanged"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Border BorderBrush="Silver" BorderThickness="1">
            <Button Content="Nice image button" Name="btnThingToClick" Width="100" Height="100" Click="btnThingToClick_Click" />
        </Border>
        <Popup Name="myPopup"
              AllowsTransparency="True"
              PlacementTarget ="{Binding ElementName=btnThingToClick}" 
              Placement="Custom">
            <Grid x:Name="grid" Height="200" Width="200" Background="Transparent">
                <Grid.RowDeFinitions>
                    <RowDeFinition Height="*"/>
                    <RowDeFinition Height="40"/>
                </Grid.RowDeFinitions>
                <Border BorderBrush="Silver" BorderThickness="1" Background="White" CornerRadius="5" Grid.Row="0" Padding="5">
                    <StackPanel Orientation="Vertical">
                        <TextBlock Text="Some stuff" />
                        <Button Content="Click me" Width="50" />
                    </StackPanel>
                </Border>
                <Path Fill="White" Stretch="Fill" stroke="Silver" HorizontalAlignment="Left" Margin="30,-1.6,0" Width="25" Grid.Row="1" 
                     Data="M22.166642,154.45381 L29.999666,187.66699 40.791059,154.54395"/>
            </Grid>
        </Popup>
    </Grid>
</Window>

这是我的主窗口代码

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;
using System.Windows.Controls.Primitives;

namespace CustomBorderStyle
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            //wire up the popup to the popup placement method
            myPopup.CustomPopupPlacementCallback = new CustomPopupPlacementCallback(placePopup);
        }

        private void btnThingToClick_Click(object sender,RoutedEventArgs e)
        {
            //just invert if it's open or not
            myPopup.IsOpen = !myPopup.IsOpen;
        }


        //this is to position the popup next to the button
        public CustomPopupPlacement[] placePopup(Size popupSize,Size targetSize,Point offset)
        {
            CustomPopupPlacement placement1 =
               new CustomPopupPlacement(new Point(10,-200),PopupPrimaryAxis.Vertical);

            CustomPopupPlacement placement2 =
                new CustomPopupPlacement(new Point(10,20),PopupPrimaryAxis.Horizontal);

            CustomPopupPlacement[] ttplaces =
                    new CustomPopupPlacement[] { placement1,placement2 };
            return ttplaces;
        }
        private void Window_LocationChanged(object sender,System.EventArgs e)
        {
            //if the popup is open when the window's location changes
            if (myPopup.IsOpen)
            {
                //toggle the popup to redraw the location
                myPopup.IsOpen = false;
                myPopup.IsOpen = true;
            }
        }
    }
}

你显然需要一些漂亮的图像用于按钮和一些更好的东西放在stackpanel,所以弹出看起来不错,但这应该做的工作:)你需要注意的是弹出窗口的位置,这意味着如果你通过添加更多东西来改变弹出窗口的高度,你需要更改“CustomPopupPlacement”对象的值,可能有一个很好的方法来修复这个??

原文地址:https://www.jb51.cc/csharp/244691.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐