在 WPF 中实现自定义/用户控件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34453603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Implementing Custom/User control in WPF
提问by Rahul
Requirement:
要求:
- As shown in image a rectangular control needs to be divided into five regions.
- It should be able to set a background color for each region.(BackgroundBrushProperty)
- A mouse click event needs to be implemented for each region.(MouseDown Event)
- 如图所示,一个矩形控件需要分为五个区域。
- 它应该能够为每个区域设置背景颜色。(BackgroundBrushProperty)
- 每个区域都需要实现一个鼠标点击事件(MouseDown Event)
Question
题
- How can I create such control in WPF?
- Can I extent existing controls for this purpose?
- What is the trick behind creating such shapes in WPF?
- 如何在 WPF 中创建这样的控件?
- 我可以为此目的扩展现有控制吗?
- 在 WPF 中创建此类形状背后的技巧是什么?
I guess creating a shape with multiple region is a big challenge in this requirement.
我想在这个要求中创建一个具有多个区域的形状是一个很大的挑战。
Can any one help me to implement this? Are there any online tutorials or articles available? Providing an example will be highly appreciated!!
任何人都可以帮助我实现这一点吗?是否有任何在线教程或文章可用?提供一个例子将不胜感激!!
.
.
回答by ApceH Hypocrite
You can extend a UserControl like below.
您可以像下面这样扩展 UserControl。
.xaml:
.xaml:
<UserControl x:Class="WpfApplication.TileBlock"
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"
xmlns:local="clr-namespace:WpfApplication"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="200"
x:Name="self" SnapsToDevicePixels="True">
<Viewbox Stretch="Fill">
<Grid Width="40" Height="50">
<Polygon Points="0,0 10,10 10,40 0,50" Fill="{Binding ElementName=self, Path=LeftBlockBackground}" Stroke="Black" StrokeThickness="0.5" MouseDown="LeftBlock_MouseDown" />
<Polygon Points="0,0 10,10 30,10 40,0" Fill="{Binding ElementName=self, Path=TopBlockBackground}" Stroke="Black" StrokeThickness="0.5" MouseDown="TopBlock_MouseDown" />
<Polygon Points="40,0 30,10 30,40 40,50" Fill="{Binding ElementName=self, Path=RightBlockBackground}" Stroke="Black" StrokeThickness="0.5" MouseDown="RightBlock_MouseDown" />
<Polygon Points="0,50 10,40 30,40 40,50" Fill="{Binding ElementName=self, Path=BottomBlockBackground}" Stroke="Black" StrokeThickness="0.5" MouseDown="BottomBlock_MouseDown" />
<Rectangle Width="20" Height="30" Fill="{Binding ElementName=self, Path=CentralBlockBackground}" Stroke="Black" StrokeThickness="0.5" MouseDown="CentralBlock_MouseDown" />
</Grid>
</Viewbox>
</UserControl>
Code-behind .cs:
代码隐藏.cs:
public partial class TileBlock : UserControl {
public TileBlock() {
InitializeComponent();
}
//Dependency properties for backgrounds
public Brush LeftBlockBackground {
get { return (Brush)GetValue(LeftBlockBackgroundProperty); }
set { SetValue(LeftBlockBackgroundProperty, value); }
}
public static readonly DependencyProperty LeftBlockBackgroundProperty =
DependencyProperty.Register("LeftBlockBackground", typeof(Brush), typeof(TileBlock), new PropertyMetadata(Brushes.Transparent));
//... repeat for Top, Right, Bottom and Central
public event MouseButtonEventHandler LeftBlockMouseDown;
private void LeftBlock_MouseDown(object sender, MouseButtonEventArgs e) {
if (LeftBlockMouseDown != null) LeftBlockMouseDown(this, e);
e.Handled = true;
}
//... repeat for Top, Right, Bottom and Central
//... repeat for MouseEnter, MouseLeave, MouseMove etc. if necessary
}
Now you can put this UserControl to yours app:
现在你可以把这个 UserControl 放到你的应用程序中:
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication2"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<local:TileBlock LeftBlockBackground="Yellow" Width="80" Height="100" LeftBlockMouseDown="TileBlock_LeftBlockMouseDown" />
</Grid>
</Window>
Good luck!
祝你好运!


