記錄一下WPF控件沒有Command屬性怎么綁定Command命令
第一步:
在相應(yīng)的項(xiàng)目 Nuget管理 安裝 System.Windows.Interactivity.WPF ,現(xiàn)在好像顯示棄用了
第二步:
在需要使用的WPF xml頁面添加引用
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
第三步:
在需要使用的WPF xml頁面添加相應(yīng)的事件并綁定到命令如下兩種:
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonDown">
<i:InvokeCommandAction Command="{Binding ClosePageCmd}" />
</i:EventTrigger>
</i:Interaction.Triggers>
<TreeView.ItemTemplate>
<HierarchicalDataTemplate DataType="{x:Type vm:MenuItemModel}" ItemsSource="{Binding SubMenuList}">
<StackPanel>
<TextBlock Text="{Binding MenuName}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonDown">
<i:InvokeCommandAction Command="{Binding Path=DataContext.MenuItemCommand,RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}" CommandParameter="{Binding Path=DataContext, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=TreeViewItem}}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBlock>
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
如果有問題請(qǐng)及時(shí)留言,僅供學(xué)習(xí)~謝謝.文章來源地址http://www.zghlxwxcb.cn/news/detail-556854.html
文章來源:http://www.zghlxwxcb.cn/news/detail-556854.html
到了這里,關(guān)于WPF事件綁定到Command命令的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!