我目前在我的自定義 UserControl 中有一個 Button,它需要將方法名稱系結到它的 Click 依賴項,該方法名稱是從用戶控制元件中的自定義依賴項屬性提供的。關于如何做到這一點的任何想法?
頁面.xaml
<local:CustomButton OnClick="CustomButton1_Click" ... />
頁面.xaml.cs
private void CustomButton1_Click(object sender, RoutedEventArgs e)
{
// do something...
}
自定義按鈕.xaml
<Button Click={x:Bind OnClick} ... />
自定義按鈕.xaml.cs
public sealed partial class CustomButton : UserControl
{
...
public static readonly DependencyProperty OnClickProperty = DependencyProperty.Register("OnClick", typeof(string), typeof(CustomButton), new PropertyMetadata(true));
public bool IsNavigator
{
get => (string)GetValue(OnClickProperty);
set => SetValue(OnClickProperty, value);
}
}
uj5u.com熱心網友回復:
你的意思是你想在點擊CustomButton1_Click時呼叫CustomButton?
自定義按鈕.xaml
<UserControl
x:Class="UserControls.CustomButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:UserControls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid>
<Button Content="Button" Click="Button_Click"/>
</Grid>
</UserControl>
自定義按鈕.xaml.cs
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using System;
namespace UserControls;
public sealed partial class CustomButton : UserControl
{
public CustomButton()
{
this.InitializeComponent();
}
public event EventHandler? OnClick;
private void Button_Click(object sender, RoutedEventArgs e)
{
OnClick?.Invoke(this, EventArgs.Empty);
}
}
并像這樣使用它:
<Grid>
<local:CustomButton OnClick="CustomButton_OnClick" />
</Grid>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/521419.html
上一篇:UnityPlayerActivity.java使用或覆寫已棄用的API。注意:使用-Xlint:deprecation重新編譯以獲取詳細資訊
