WPF自定义命令(转)
WPF自定義命令
自定義命令,可以分為兩個層次來理解
1.聲明自己的RoutedCommand實例,這個層次比較淺
2.從實現(xiàn)ICommand接口開始,這個才算的上真正的自定義命令
自定義命令的目的是為了把業(yè)務(wù)邏輯轉(zhuǎn)移到命令內(nèi)部,而不是需要捕捉到命令之后再回過頭去處理。
要想自定義命令,就不能不了解命令系統(tǒng)的幾個要素:
1.命令(Command):WPF的命令實際上就是實現(xiàn)了ICommand接口的類,平時使用最多的就是RoutedCommand類.
2.命令源(Command Source):命令的發(fā)送者,是實現(xiàn)了ICommandSource的類,平時用的Button就實現(xiàn)了這個接口
3.命令目標(biāo)(Command Target):即命令將發(fā)送給誰,或者說命令的作用對象,是實現(xiàn)了IINputElement的類
4.命令關(guān)聯(lián)(Command Binding):負責(zé)把一些外圍邏輯與命令關(guān)聯(lián)起來,比如執(zhí)行之前對命令是否可以執(zhí)行進行判斷,命令執(zhí)行之后還有那些后續(xù)工作
了解了命令系統(tǒng)的幾個要素,就可以自己定義命令了
1.定義接口
為了規(guī)范我們的命令源,更統(tǒng)一的通過命令來調(diào)用,我們約定一個接口,所有使用該命令的命令源必須是實現(xiàn)了該接口的類型。
interface IView {
//是否更改
bool IsChanged { get; set; }
//方法
void SetBinding();
void Refresh();
void Clear();
void Save();
}
2.定義命令
創(chuàng)建一個專門用于處理IView接口派生類的命令。我們說,命令就是實現(xiàn)了ICommand接口的類,接口包含兩個方法,一個委托事件,我們這里只實現(xiàn)Excute方法
class ClearCommand:ICommand
{
public bool CanExecute(object parameter)
{
throw new NotImplementedException();
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
IView view = parameter as IView;
//這里體現(xiàn)了我們的業(yè)務(wù)邏輯
view.Clear();
}
}
3.定義命令源
有了命令,下一步定義發(fā)送命令的命令源
class MyCommandSource:UserControl,ICommandSource
{
//命令
public ICommand Command{ get;set; }
//參數(shù)
public object CommandParameter { get; set; }
//目標(biāo)
public System.Windows.IInputElement CommandTarget { get; set; }
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
base.OnMouseLeftButtonDown(e);
//執(zhí)行命令
if (this.Command != null)
{
//目標(biāo)作為參數(shù)
Command.Execute(this.CommandTarget);
}
}
}
4.定義命令的目標(biāo)
命令目標(biāo)有兩個限制,一個是實現(xiàn)IInputElement,另一個是我們定義的,要求實現(xiàn)接口IView
分為兩部分 一部分是XAML
<UserControl x:Class="CommandTest2.MiniView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="120" Width="200">
<Grid>
<Border CornerRadius="5" BorderBrush="LawnGreen" BorderThickness="2">
<StackPanel>
<TextBox x:Name="txt1" Margin="5"></TextBox>
<TextBox x:Name="txt2" Margin="5"></TextBox>
<TextBox x:Name="txt3" Margin="5"></TextBox>
<TextBox x:Name="txt4" Margin="5"></TextBox>
</StackPanel>
</Border>
</Grid>
</UserControl>
另一部分是后臺代碼
namespace CommandTest2
{
/// <summary>
/// MiniView.xaml 的交互邏輯
/// </summary>
public partial class MiniView : UserControl,IView
{
public MiniView()
{
InitializeComponent();
}
public bool IsChanged
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
public void SetBinding()
{
throw new NotImplementedException();
}
public void Refresh()
{
throw new NotImplementedException();
}
public void Save()
{
throw new NotImplementedException();
}
public void Clear()
{
txt1.Clear();
txt2.Clear();
txt3.Clear();
txt4.Clear();
}
}
}
這樣,我們需要的幾個部分湊齊,就可以使用了
窗體代碼
<Window x:Class="CommandTest2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:CommandTest2"
Title="Command" Height="205" Width="250">
<StackPanel>
<local:MyCommandSource x:Name="ctrlClear" Margin="10">
<TextBlock Text="清除" FontSize="16" TextAlignment="Center"
Background="LightGreen" Width="80"></TextBlock>
</local:MyCommandSource>
<local:MiniView x:Name="miniView"></local:MiniView>
</StackPanel>
</Window>
<Window x:Class="CommandTest2.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local="clr-namespace:CommandTest2"Title="Command" Height="205" Width="250"><StackPanel><local:MyCommandSource x:Name="ctrlClear" Margin="10"><TextBlock Text="清除" FontSize="16" TextAlignment="Center"Background="LightGreen" Width="80"></TextBlock></local:MyCommandSource> <local:MiniView x:Name="miniView"></local:MiniView></StackPanel> </Window>
后臺調(diào)用也很簡單
ClearCommand clrCommand = new ClearCommand();ctrlClear.Command = clrCommand; ctrlClear.CommandTarget = miniView;ClearCommand clrCommand = new ClearCommand();ctrlClear.Command = clrCommand; ctrlClear.CommandTarget = miniView;
?
參考《深入淺出WPF》劉鐵猛 第9章
?
ps.工作需要,開始學(xué)習(xí)WPF,感覺跟ASP.NET還是有很多不一樣的,在這里記錄一下學(xué)習(xí)的歷程,以便今后回顧之用,看如何一步步走的,走向何方
?
轉(zhuǎn)載于:https://www.cnblogs.com/LiZhongZhongY/p/10896668.html
總結(jié)
以上是生活随笔為你收集整理的WPF自定义命令(转)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java JTable3
- 下一篇: js new