Button Enablity WPF

本文关键字:WPF Enablity Button | 更新日期: 2023-09-27 18:23:47

我有一个UserControl,我有一个DataDrid,在这个DataGrid我有两个ComboBoxes。现在我想做的是当我从ComboBoxes选择任何项目时,应该启用DataGrid之外的按钮。

我的DataGrid绑定到ItemSource组合框也是如此。

我尝试使用MuliDatatriggers但他们失败了,因为按钮在DataGrid外面,所以这些ComboBoxes将不可用。

<DataGrid>
   <DataGrid.Columns>
      <DataGridTemplateColumn Width="Auto">
         <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
               <ComboBox Name="Combo1" ItemsSource="{Binding Lst1,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" DisplayMemberPath="Code1" SelectedValue="{Binding CodeID1,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">      
               <ComboBox Name="Combo2" ItemsSource="{Binding Lst2,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" DisplayMemberPath="Code2" SelectedValue="{Binding CodeID2,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">
            </DataTemplate>
         </DataGridTemplateColumn.CellTemplate>
      </DataGridTemplateColumn>
   </DataGrid.Columns>
</DataGrid>
<Button Name="Add" IsEnabled="{Binding IsAddEnabled,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>

Button Enablity WPF

这个问题已经有很多答案。

例如:选择组合框项时启用文本框

更好的方法是将 MVVM 应用于应用程序。

我同意MVVM@MikroDel,即在wpf中正常工作的唯一方法。我做了这样的事情,但不要使用两个 cmb,也不在 datagrid 上,但这根本不需要不同,因为在每个组合中,您将所选索引设置为您在 viewModel 上的属性,并且按钮相同。

在这个例子中,我使用RelayCommand你可以阅读Hare关于使用它,但不是这个Q主题。

此外,我使用转换器'因为如果选择索引= 0,也可以启用按钮,因此它实现得非常简单

 namespace MCSearchMVVM
 {
     class MCBindButtonToComboBox : IValueConverter
     {
         public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
         {
            if (value == null) 
               return false;
           return true;
          }
          public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
          {            throw new NotImplementedException();        }
      }
 }

现在来看真实的东西;)在此之前的小建议是我喜欢将视图(.xaml 文件(和 vm(.cs 文件(放在同一个文件夹中,这就是为什么我发现这个例子非常快,哈哈

首先,我们提出以下观点:

<UserControl x:Class="MCSearchMVVM.AddFilePage"    
         ...
         xmlns:local="clr-namespace:MCSearchMVVM"
         ...>
<UserControl.Resources>
    <local:MCBindButtonToComboBox x:Key="enableCon"/>
</UserControl.Resources>
<Grid>
    <Grid.ColumnDefinitions>
       ...
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        ...
    </Grid.RowDefinitions>
    <Grid.Background>
        ...
    </Grid.Background>
    <Button Content="Browse.." 
            ...
            Command="{Binding BrowseCommand}"          
            IsEnabled="{Binding FileKindIndexSelected,
                        Converter={StaticResource enableCon}}"
            .../>
    <ComboBox ... SelectedIndex="{Binding FileKindIndexSelected, Mode=TwoWay}" ... >
        ...
    </ComboBox>
   ...
</Grid>

现在视图模型:

public class AddFileViewModel : ObservableObject, IPageViewModel
{
    ...
    private int _fileKindIndexSelected;
    public int FileKindIndexSelected
    {
        get { return _fileKindIndexSelected; }
        set { SetField(ref _fileKindIndexSelected, value, "FileKindIndexSelected");}
    }
    ...
 }

和 SetField 函数

 public abstract class ObservableObject : INotifyPropertyChanged
 {
    [Conditional("DEBUG")]
    [DebuggerStepThrough]
    public virtual void VerifyPropertyName(string propertyName)
    {
        if (TypeDescriptor.GetProperties(this)[propertyName] == null)
        {
            string msg = "Invalid property name: " + propertyName;
            if (this.ThrowOnInvalidPropertyName)
                throw new Exception(msg);
            else
                Debug.Fail(msg);
        }
    }
     protected virtual bool ThrowOnInvalidPropertyName { get; private set; }
     #region INotifyPropertyChanged
    public virtual void RaisePropertyChanged(string propertyName)
    {
        this.VerifyPropertyName(propertyName);
        OnPropertyChanged(propertyName);
    }
    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = this.PropertyChanged;
        if (handler != null)
        {
            var e = new PropertyChangedEventArgs(propertyName);
            handler(this, e);
        }
    }
    protected bool SetField<T>(ref T field, T value, string propertyName)
    {
        if (EqualityComparer<T>.Default.Equals(field, value))
            return false;
        field = value;
        OnPropertyChanged(propertyName);
        return true;
    }
    #endregion // INotifyPropertyChanged
 }
}

我希望这个方向是有帮助的。对不起,我的英语不好=((