[英]Inactive commands in wpf Toolbar
我有帶有工具欄(添加和刪除命令)和TabControl的應用程序。 TabControl的每個tabItem中都有VariableGrid控件。
在以下位置查看圖片: http : //trueimages.ru/view/cNFyf
<DockPanel >
<ToolBarTray DockPanel.Dock="Top">
<ToolBar>
<Button Command="{x:Static VariableGrid:VariableGrid.AddRowCommand}"/>
<Button Content="Delete" Command="ApplicationCommands.Delete" />
</ToolBar>
</ToolBarTray>
<TabControl x:Name="tc">
<TabItem Header="Tab 1">
<vg:VariableGrid ItemsSource="{Binding Items1, Mode=TwoWay}"/> </TabItem>
<TabItem Header="Tab 2">
<vg:VariableGrid ItemsSource="{Binding Items2, Mode=TwoWay}"/>
</TabItem>
</TabControl>
<DockPanel >
工具欄命令在我的控件中實現:
public partial class VariableGrid : DataGrid, INotifyPropertyChanged
{
public static RoutedCommand AddRowCommand = new RoutedCommand();
public VariableGrid()
{
this.CommandBindings.Add(new CommandBinding(VariableGrid.AddRowCommand, AddRow));
this.CommandBindings.Add(new CommandBinding(ApplicationCommands.Delete, R emoveRow, CanRemoveRow));
}
private void AddRow(object sender, ExecutedRoutedEventArgs e)
{
…
}
private void RemoveRow(object sender, ExecutedRoutedEventArgs e)
{
…
}
private void CanRemoveRow(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = (SelectedItems.Count > 0);
}
}
在少數情況下,禁用工具欄中的命令:
選擇DataGrid的任何行時-工具欄的命令將變為活動狀態。
您能幫我解決我的問題嗎? 我應該設置工具欄按鈕的哪個CommandTarget?
PS:我的應用程序中有兩個VariableGrid。 這就是為什么我不能將CommandTarget設置為“ {Binding ElementName = variableGrid}”的原因。 我認為應該將其設置為FocusedElement。 但是我不知道該怎么做。
WPF應該CanRemoveRow
地調用您的CanRemoveRow
方法,以檢查是否可以刪除行。 您應該在此方法中放置一個布爾條件,它將回答該問題。 如果您希望AddRowCommand
具有類似的功能,請在綁定AddRowCommand
位置添加CanAddRow
方法。
您可能需要閱讀MSDN上的Commanding Overview 。
更新>>>
哦...您想知道在這些禁用條件下使用什么代碼嗎? 我會這樣假設:
當應用程序運行時
我猜你的意思是“當應用程序忙時” ...添加一個名為IsBusy
的布爾屬性,當應用程序執行任何長時間運行的進程時將其設置為true,然后將!IsBusy
添加到您的方法條件中。
當我單擊DataGrid的灰色字段時
當DataGrid為空時
通過在方法條件中添加&& dataGrid.SelectedItem != null
可以使用DataGrid
的SelectedItem
屬性來判斷這兩個條件。
因此,您需要以下內容:
private void CanRemoveRow(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = !IsBusy && SelectedItem != null);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.