自定义控制按钮 C# UWP
Posted
技术标签:
【中文标题】自定义控制按钮 C# UWP【英文标题】:Custom Control Button C# UWP 【发布时间】:2021-02-26 10:30:48 【问题描述】:我需要一些有关自定义控件的帮助。我创建了一个 IconButton 以绑定一些符号,然后我尝试制作类似开关按钮的东西,但符号这是我的类。
public sealed partial class IconButton : UserControl
private SolidColorBrush _symbolColor;
public SolidColorBrush SymbolColor
get return _symbolColor;
set
SI.Foreground = value;
_symbolColor = value;
public Ways _way;
public Ways Way
get return _way;
set _way = value;
private bool _autoChangeColor = false;
public bool AutoChangeColor
get return _autoChangeColor;
set _autoChangeColor = value;
private bool _cursor = false;
public bool Cursor
get return _cursor;
set
if(value)
BT.Click += Click;
_cursor = value;
private bool _reverse = false;
public bool Reverse
get return _reverse;
set
_reverse = value;
public void Click(object sender, RoutedEventArgs e)
ChangeCursor();
private void InitCursor()
if (!Reverse)
if (Symbol == Symbol.Forward)
Way = Ways.Left;
else if (Symbol == Symbol.Back)
Way = Ways.Right;
else if (Symbol == Symbol.Accept)
if (AutoChangeColor)
SI.Foreground = new SolidColorBrush(Colors.Green);
Way = Ways.Cancel;
else if (Symbol == Symbol.Cancel)
if (AutoChangeColor)
SI.Foreground = new SolidColorBrush(Colors.Red);
Way = Ways.Accept;
else if (Symbol == Symbol.FullScreen)
Way = Ways.BackToWindow;
else if (Symbol == Symbol.BackToWindow)
Way = Ways.FullScreen;
else
if (Symbol == Symbol.Forward)
Way = Ways.Right;
else if (Symbol == Symbol.Back)
Way = Ways.Left;
else if (Symbol == Symbol.Accept)
if (AutoChangeColor)
SI.Foreground = new SolidColorBrush(Colors.Green);
Way = Ways.Accept;
else if (Symbol == Symbol.Cancel)
if (AutoChangeColor)
SI.Foreground = new SolidColorBrush(Colors.Red);
Way = Ways.Cancel;
else if (Symbol == Symbol.FullScreen)
Way = Ways.FullScreen;
else if (Symbol == Symbol.BackToWindow)
Way = Ways.BackToWindow;
private void ChangeCursor()
if(Cursor)
if (!Reverse)
if (Symbol == Symbol.Forward)
Symbol = Symbol.Back;
Way = Ways.Right;
else if (Symbol == Symbol.Back)
Symbol = Symbol.Forward;
Way = Ways.Left;
else if (Symbol == Symbol.Accept)
if (AutoChangeColor)
SI.Foreground = new SolidColorBrush(Colors.Red);
Symbol = Symbol.Cancel;
Way = Ways.Accept;
else if (Symbol == Symbol.Cancel)
if (AutoChangeColor)
SI.Foreground = new SolidColorBrush(Colors.Green);
Symbol = Symbol.Accept;
Way = Ways.Cancel;
else if (Symbol == Symbol.FullScreen)
Symbol = Symbol.BackToWindow;
Way = Ways.FullScreen;
else if (Symbol == Symbol.BackToWindow)
Symbol = Symbol.FullScreen;
Way = Ways.BackToWindow;
else
if (Symbol == Symbol.Forward)
Symbol = Symbol.Back;
Way = Ways.Left;
else if (Symbol == Symbol.Back)
Symbol = Symbol.Forward;
Way = Ways.Right;
else if (Symbol == Symbol.Accept)
if (AutoChangeColor)
SI.Foreground = new SolidColorBrush(Colors.Red);
Symbol = Symbol.Cancel;
Way = Ways.Cancel;
else if (Symbol == Symbol.Cancel)
if (AutoChangeColor)
SI.Foreground = new SolidColorBrush(Colors.Green);
Symbol = Symbol.Accept;
Way = Ways.Accept;
public IconButton()
this.InitializeComponent();
public Symbol _symbol;
public Symbol Symbol
get return _symbol;
set
SI.Symbol = value;
_symbol = value;
private void BT_Loaded(object sender, RoutedEventArgs e)
if(Cursor)
InitCursor();
<UserControl
x:Class="DosFab.App.CustomControl.IconButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:DosFab.App.CustomControl"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
MinHeight="30"
MinWidth="30">
<Button Name="BT" Padding="0" MinHeight="30" MinWidth="30" HorizontalAlignment="Left" VerticalAlignment="Top" Loaded="BT_Loaded">
<SymbolIcon Name="SI"/>
</Button>
</UserControl>
现在我在另一个页面中使用这个按钮,当我像这样绑定Way
<customcontrol:IconButton Symbol="FullScreen" Cursor="True" Way="x:Bind Path=ViewModel.Way, Mode=TwoWay"/>
VisualStudio 给我这个错误 TwoWay 绑定目标“Way”必须是一个依赖属性,我可以'不要也使用 Click 事件。
感谢阅读,对不起我的英语
【问题讨论】:
【参考方案1】:正如错误消息所示,您需要在 IconButton
类中将您的 Way 属性设置为 DependencyProperty。依赖属性是存在于 DependencyProperty 标识符上的属性。 DependencyProperty 标识符通常是用于标识属性的常量值,它通常是公共的和只读的。
像这样添加Way
属性的依赖属性标识符的定义:
IconButton.xaml.cs
……
public static readonly DependencyProperty WayProperty = DependencyProperty.Register(
"Way",typeof(Ways),typeof(IconButton),new PropertyMetadata(null));
然后,您可以使用双向绑定并且单击事件可以工作。 如果您有任何其他问题,请随时与我们联系。
【讨论】:
感谢您的帮助,我找到了如何使用这个 DependencyProperty。你真的再帮我一次!以上是关于自定义控制按钮 C# UWP的主要内容,如果未能解决你的问题,请参考以下文章