2014-05-14 185 views
0

我有一個視圖,其中包含按鈕形式的各種項目和每個按鈕的複選框。 XAML:勾選/取消勾選複選框時啓用,禁用按鈕

 <ListBox.ItemTemplate> 
      <DataTemplate> 
       <Grid> 
        <Grid.ColumnDefinitions> 
         <ColumnDefinition /> 
         <ColumnDefinition /> 
        </Grid.ColumnDefinitions> 
        <CheckBox x:Name="CheckFavorite" 
           Width="Auto" 
           Height="Auto" 
           AutomationProperties.AutomationId="AID_FavoritesCheck" 
           IsChecked="{Binding Path=IsChecked, 
                Mode=TwoWay}" 
           Visibility="{Binding IsFavoriteConfiguredAndInDA, 
                Converter={StaticResource boolToVisibility}}" 
           Checked="OnContentChanged" 
           Unchecked="OnContentChanged"/> 
        <Button Grid.Column="1" 
          Width="240" 
          HorizontalContentAlignment="Left" 
          VerticalContentAlignment="Center" 
          AutomationProperties.AutomationId="AID_BtnLaunchFavorite" 
          Command="{Binding Path=LaunchFavorite}" 
          Content="{Binding Path=ModuleDisplayName}" 
          Cursor="Hand" 
          FontSize="{StaticResource UxLevel_5}" 
          Padding="24 12 2 12" 
          ToolTip="{Binding Path=ModuleDisplayName}" /> 
       </Grid> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 

我有另一個叫做「Launch」的按鈕。它的基本功能是發射任務按選擇在上面的列表框項目,使多個任務可以launched.Button XAML:

 <Button Name="btnLaunch" 
       Width="80" 
       Height="25" 
       HorizontalAlignment="Left" 
       AutomationProperties.AutomationId="AUI_BtnLaunchForFavorite" 
       Command="{Binding LaunchCommand}" 
       Style="{StaticResource LaunchButtonStyle}" 
       Visibility="{Binding IsRIAMode, 
            Converter={StaticResource boolToVisibilityInverter}}"> 

問題: 我想啓用/如果ATLEAST一個禁用此啓動按鈕項目被選中並禁用其他方式。我可以如何實現?請幫助一些代碼片段。

UPDATE: 這裏是啓動命令:

public ICommand LaunchCommand { get; private set; } 
LaunchCommand = new DelegateCommand<object>(OnLaunch); 
     internal void OnLaunch(object sender) 
     { 
      var nFavItemToLaunchCount = Favorites.Count(favItem => favItem.IsChecked); 

      if (!IsSessionLimitReached(nFavItemToLaunchCount)) 
      { 
       foreach (FavoriteItemViewModel favoriteItem in Favorites) 
       { 
        if (favoriteItem.IsChecked) 
        { 
         favoriteItem.LaunchFavorite.Execute(sender); 

        } 
       } 
      } 
     } 

更新2:

我所做的更改按克里希納的評論:

LaunchCommand = new DelegateCommand<object>(OnLaunch,ToggleLaunch); 
private bool ToggleLaunch(object obj) 
{ 

     if (Favorites.Count(i => i.IsChecked)!=0) //Favorites is the itemsource 
     { 
       return true; 
     } 
      return false; 
} 

儘管如此發射按鈕顯示禁用即使在選擇/檢查項目時也是如此。

更新3

Krishna的進一步的評論之後,我改變了性質的器isChecked實施以及在實施viewmodel.Still沒有INotifyPropertyChanged的運氣!

public class FavoriteItemViewModel:INotifyPropertyChanged 
{ 
     public bool IsChecked 
     { 
      get 
      { 
       return m_IsChecked; 
      } 
      set 
      { 
       m_IsChecked = value; 
       OnPropertyChanged("IsChecked"); 
      } 
     } 

     /// <summary> 
     /// Handler for property change 
     /// </summary> 
     /// <param name="propertyName">Name of property</param> 
     protected void OnPropertyChanged(string propertyName) 
     { 
      PropertyChangedEventHandler handler = PropertyChanged; 

      if (handler != null) 
      { 
       handler(this, new PropertyChangedEventArgs(propertyName)); 
      } 
     } 
} 

還有另一種視圖模型「FavoriteContainerViewModel」持有「FavoriteItemViewModel」的集合,也實現INotifyPropertyChanged。此容器視圖模型是其中i加入UPDATE提到的代碼的地點2

UPDATE 4: 執行收藏:

public class FavoriteContainerViewModel : ViewModelBase, IModuleView 
{ 
    private readonly ObservableCollection<FavoriteItemViewModel> m_Favorites = new ObservableCollection<FavoriteItemViewModel>(); 

     public ObservableCollection<FavoriteItemViewModel> Favorites 
     { 
      get { return m_Favorites; } 
     } 
     public FavoriteContainerViewModel() 
     { 
      LaunchCommand = new DelegateCommand<object>(OnLaunch, ToggleLaunch); 
      OnLoad(); 
     } 
     private bool ToggleLaunch(object obj) 
     { 
      if (Favorites.Count(i => i.IsChecked) != 0) 
      { 
       return true; 
      } 
      return false; 
      //return true; 
     } 
} 

注:ViewModelBase實現INotifyPropertyChanged

更新5: 問題現在使用基於事件的模型解決。實現以下幾點: FavoriteViewModel.cs

public event EventHandler ItemChecked; 
public bool IsChecked 
{ 
    get 
    { 
     return m_IsChecked; 
    } 
    set 
    { 
     m_IsChecked = value; 
     if (ItemChecked != null) 
     { 
      ItemChecked(this, new EventArgs()); 
     } 
    } 
} 

FavoriteItemContainerViewModel.cs

private void SubscribeFavoriteItemEvents(FavoriteItemViewModel favorite) 
{ 
    favorite.ItemChecked += ToggleLaunchButton; 
} 

private bool m_IsLaunchEnabled; 
public bool IsLaunchEnabled 
{ 
    get 
    { 
     return m_IsLaunchEnabled; 
    } 
    set 
    { 
     m_IsLaunchEnabled = value; 
     OnPropertyChanged("IsLaunchEnabled"); 
    } 
} 

物業IsLaunchEnabled被綁定到按鈕啓用/禁用。

+0

什麼是'LaunchCommand'? – amnezjak

+0

單擊此按鈕啓動LaunchCommand並啓動選定的任務。 – Sandeep

+0

是的,但是它的實例是什麼類?你自己的'ICommand'實現還是一些工具包類?你有'CanExecute'方法嗎?如果是這樣,啓用/禁用該方法中的按鈕。 – amnezjak

回答

1

您可以啓用/禁用基礎上,CheckBox.IsChecked屬性Button

<Button Name="btnLaunch" 
       Width="80" 
       Height="25" 
       HorizontalAlignment="Left" 
       AutomationProperties.AutomationId="AUI_BtnLaunchForFavorite" 
       Command="{Binding LaunchCommand}" 
       Style="{StaticResource LaunchButtonStyle}" 
       Visibility="{Binding IsRIAMode, 
            Converter={StaticResource boolToVisibilityInverter}}" 
       IsEnabled="{Binding ElementName=CheckFavorite, Path=IsChecked}"> 
+0

這是行不通的 – Sandeep

+0

在同一個可視化樹中是否有'Button'和'Checkbox'? –

+0

不,複選框是列表框的一部分,而按鈕是完全不同的堆棧面板 – Sandeep

0
if (checkbox1.Checked && !checkbox2.Checked) 
    { 
     button1.Enable = true 
    } 
    else if (!checkbox1.Checked && checkbox2.Checked) 
    { 
     button1.Enable = false 
    } 
1

我猜發射按鈕是列表框之外,用戶點擊一次的複選框被選中。

在這種情況下,你需要在你的ICommand(LaunchCommand)的「CanExecute」部分

讓我們假設在構造函數中的ICommand的實現添加一個條件是一樣的東西

LaunchCommand = new RelayCommand(launchMethod); 
private void launchMethod(object obj) 
{ 
//do some work here 
} 

現在添加canExecute一部分,你的命令通過改變初始化

LaunchCommand = new DelegateCommand<object>(OnLaunch,checkCheckboxes); 

,並添加這個方法

private bool checkCheckboxes(object obj) 
{ 
    //YourList is your itemsource 
    return YourList.Where(i=>i.IsChecked).Any(); 
} 

只需更改上面的方法來滿足您的需求

更新您的財產器isChecked更改爲以下

private bool isChecked; 
     public bool IsChecked 
     { 
      get { return isChecked; } 
      set 
      { 
       isChecked = value; 
       OnPropertyChanged("IsChecked"); 
      } 
     } 
+0

請參閱我的更新啓動命令 – Sandeep

+0

然後更改您的實現像LaunchCommand = new DelegateCommand (OnLaunch,checkCheckboxes);並添加我的方法在我的帖子中提到checkCheckboxes。您只需確保至少檢查一個項目,如果是,則返回true,如果沒有項目被選中,則返回false。這將啓用和禁用您的啓動按鈕自動 – Krishna

+0

您可以請幫我與代碼片段檢查至少一個選定的項目?我是WPF的新手 – Sandeep