【问题标题】:Button Enablity WPF按钮启用 WPF
【发布时间】:2013-07-16 23:37:49
【问题描述】:

我有一个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}"/>

【问题讨论】:

  • Farzi - 如果答案对您有帮助 - 请点赞。如果这是解决方案 - 也接受它
  • 因为今天你有 6 个问题和 0 个接受的答案!
  • 没有。您提供的网址没有回答我的问题。我的按钮在 Datagrid 之外,因此使用 DataTriggers 并为 Combobox 提供元素名称将无济于事,因为它们在 datagrid 内,而按钮在 dataGrid 之外。所以它不会有帮助。但是如果我们可以使用相对源属性可能会有所帮助..但我不知道如何在数据触发器中使用它,因为每当我使用它时都会给我一个异常
  • 你可以在你的应用程序中使用 MVVM 吗?这样您就可以在 ViewModel 中实现启用逻辑
  • 我正在使用 MVVM,您可以看到我在问题中提到的 TAG。这是我正在谈论的用户控件。所以所有功能都在代码后面完成

标签: c# wpf data-binding mvvm user-controls


【解决方案1】:

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

例如:Enable text box when combobox item is selected

对您来说更好的方法是将MVVM 应用于您的应用程序。

【讨论】:

    【解决方案2】:

    我同意@MikroDel 与 MVVM 的观点,即 only 在 wpf 中正常工作的方式。我做这样的事情,但不是使用两个 cmb,也不是在 datagrid 上,但不需要不同完全是因为在每个组合中,您将所选索引设置为 viewModel 上的属性,而按钮也是如此。

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

    此外,我使用了一个转换器,因为如果选择 index = 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>
    

    现在是 ViewModel:

    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
     }
    }
    

    我希望这个方向会有所帮助.. 对不起我的英语不好=))

    【讨论】:

      猜你喜欢
      • 2015-04-19
      • 2013-11-23
      • 1970-01-01
      • 1970-01-01
      • 2011-01-26
      • 2018-01-04
      • 2012-12-16
      • 2014-11-05
      • 2021-05-18
      相关资源
      最近更新 更多