将Button.IsEnabled绑定到ComboBox.SelectedItem [英] Binding Button.IsEnabled to ComboBox.SelectedItem

查看:113
本文介绍了将Button.IsEnabled绑定到ComboBox.SelectedItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将TextBox.IsEnabled属性绑定到ComboBox的SelectedItem.由于在ComboBox中未选择任何内容,因此显示表单时,搜索"按钮被禁用,因此我处于中间位置.但是,当我在ComboBox中进行选择时, 搜索按钮未重新启用.在组合框内进行选择时,如何使搜索按钮启用?代码如下.

I am trying to bind the TextBox.IsEnabled property to the SelectedItem of a ComboBox.  I'm half way there as my Search button is disabled when the form displays because of no selection in the ComboBox.  However, when I make a selection in the ComboBox, the Search button is not re-enabled.  How can I get the Search button to enable when I make a selection in the ComboBox?  The code is below.

谢谢

凯尔

 

XAML代码

<Window x:Class="ValidationTest.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:converter="clr-namespace:ValidationTest"
  Title="Window1" Height="300" Width="300">
  
  <Window.Resources>
    <converter:ComboBoxSelectedItemConverter x:Key="ComboBoxSelectedItemConverter"></converter:ComboBoxSelectedItemConverter>
  </Window.Resources>
  
  <StackPanel>
    <ComboBox Name="cbFields" DisplayMemberPath="FieldName" SelectedValuePath="FieldValue"></ComboBox>
    <Button Name="btnAdd" Content="Add" Click="btnAdd_Click" ></Button>
    <Button Name="btnSearch" Content="Search" Click="btnSearch_Click" IsEnabled="{Binding ElementName=cbFields, Path=SelectedItem, Converter={StaticResource ComboBoxSelectedItemConverter} }"></Button>
  </StackPanel>
  
</Window>

C#代码

    private void btnAdd_Click(object sender, RoutedEventArgs e)
    {
      IList<Field> fields = new List<Field>();

      fields.Add(new Field { FieldName = "Configuration Filter", FieldValue = "ConfigurationFilter" });
      fields.Add(new Field { FieldName = "Pacakge Name", FieldValue = "PackageName" });

      cbFields.ItemsSource = fields;

      btnSearch.IsEnabled = false;

    }

    private void btnSearch_Click(object sender, RoutedEventArgs e)
    {

    }




namespace ValidationTest
{
  public class ComboBoxSelectedItemConverter : IValueConverter
  {
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
      if (value is ComboBoxItem)
      {
        return true;
      }
      return false;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
      throw new NotImplementedException();
    }

    #endregion
  }
}

 

推荐答案

在您的值转换器中,如果value为null,则尝试返回false;否则,返回false.否则返回true.

In your value converter, try return false if value is null; return true otherwise.

示例:

< Window x:Class =" BindButtonToComboBoxHasSelectedItem.MainWindow"
       xmlns =" http://schemas.microsoft.com/winfx/2006/xaml/presentation " ;
       xmlns:x =" http://schemas.microsoft.com/winfx/2006/xaml "
       xmlns:local ="clr-namespace:BindButtonToComboBoxHasSelectedItem"
      标题="MainWindow".高度="350".宽度="525">
    < Window.Resources>
       < local:MyConverter x:Key ="myConverter"/>
    </Window.Resources>
    < StackPanel>
       < ComboBox Name ="combobox" ItemsSource ="{Binding}"/>
       <按钮内容=搜索"; IsEnabled =""{Binding SelectedItem,ElementName = combobox,Converter = {StaticResource myConverter}}""/>
    </StackPanel>
</Window>

<Window x:Class="BindButtonToComboBoxHasSelectedItem.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:BindButtonToComboBoxHasSelectedItem"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <local:MyConverter x:Key="myConverter"/>
    </Window.Resources>
    <StackPanel>
        <ComboBox Name="combobox" ItemsSource="{Binding}"/>
        <Button Content="Search" IsEnabled="{Binding SelectedItem, ElementName=combobox, Converter={StaticResource myConverter}}"/>
    </StackPanel>
</Window>

使用系统;
使用System.Collections.Generic;
使用System.Globalization;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

命名空间BindButtonToComboBoxHasSelectedItem
{
   公共局部类MainWindow:Window
    {
      公共MainWindow()
       {
           InitializeComponent();
           DataContext =新列表< string>
           {
                             " Item1",
                             " Item2",
                             " Item3",
           };
           Console.WriteLine(new ComboBox().SelectedItem);
       }
    }
   公共类MyConverter:IValueConverter
    {
      公共对象Convert(对象值,类型targetType,对象参数,CultureInfo文化)
       {
            if(value == null)返回false;
          返回true;
       }

namespace BindButtonToComboBoxHasSelectedItem
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = new List<string>
            {
                "Item1",
                "Item2",
                "Item3",
            };
            Console.WriteLine(new ComboBox().SelectedItem);
        }
    }
    public class MyConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null) return false;
            return true;
        }

      公共对象ConvertBack(对象值,类型targetType,对象参数,CultureInfo文化)
       {
          抛出新的NotImplementedException();
       }
    }
}

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}


这篇关于将Button.IsEnabled绑定到ComboBox.SelectedItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆