C# 如何确定是否选择了WPF Datagrid项,并绑定到true/false结果

C# 如何确定是否选择了WPF Datagrid项,并绑定到true/false结果,c#,wpf,xaml,datagrid,C#,Wpf,Xaml,Datagrid,我觉得这应该很容易,但我被难住了 我正在尝试将ComboBox.IsEnabled属性绑定到某个对象,例如当前使用MVVM在DataGrid属性上选择的项。这样,如果DataGrid上未选择任何项,则组合框将被禁用 是否有一个DataGrid属性在选择某个项时注册True/False,或者我是否需要对SelectedItems.Count属性执行某些操作 在编写转换器或自定义属性之前,我试图用尽可能少的代码来完成这项工作。我相信没有内置属性会表示在DataGrid中选择了一项。相反,您可以将属性

我觉得这应该很容易,但我被难住了

我正在尝试将ComboBox.IsEnabled属性绑定到某个对象,例如当前使用MVVM在DataGrid属性上选择的项。这样,如果DataGrid上未选择任何项,则组合框将被禁用

是否有一个DataGrid属性在选择某个项时注册True/False,或者我是否需要对SelectedItems.Count属性执行某些操作


在编写转换器或自定义属性之前,我试图用尽可能少的代码来完成这项工作。

我相信没有内置属性会表示在DataGrid中选择了一项。相反,您可以将属性绑定到DataGrid的SelectedItem,并检查SelectedItem是否为null

例如:

 <DataGrid
        ItemsSource="{Binding ListOfitems}" 
        SelectedItem="{Binding CurrentItem, Mode=TwoWay}"/> 
//xaml


我最终用一个转换器来解决上述问题。谢谢大家的建议。我只是想确保我没有丢失一个属性,在我实现这个之前

XAML

代码隐藏

public class ItemToBoolConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        try
        {
            int? itemCount = value as int?;
            if (itemCount < 1)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        catch { return DependencyProperty.UnsetValue; }
    }
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotSupportedException();
    }

}  
看法

转换器:

 public class SelectionConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {

        return value != null;
    }

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

没有足够的信息,MVVM或代码隐藏?MVVM。很抱歉,我将编辑我的问题。添加一些您迄今为止尝试过的好方法的代码。唯一的问题是您需要指定一个ElementName。休息很好。
public bool IsItemSelected { get {return null != SelectedModelItem; }

public YourModelType SelectedModelItem{
get{
   return selectedModelItem;
}
set{
   selectedModelItem = value;
   OnPropertyChanged();   
   }
}
<Control.Resources>
    <local:ItemToBoolConverter x:Key="ItemToBoolConverter"/>
</Control.Resources>

 <ComboBox IsEnabled="{Binding ElementName=dataGrid, Path=SelectedItems.Count, Converter={StaticResource ItemToBoolConverter}}">
public class ItemToBoolConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        try
        {
            int? itemCount = value as int?;
            if (itemCount < 1)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        catch { return DependencyProperty.UnsetValue; }
    }
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotSupportedException();
    }

}  
<Window x:Class="..."
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:cvt="clr-namespace:TestTelerikColumnFooter"
    Width="300" Height="300" 
    >
<Window.Resources>
    <cvt:SelectionConverter x:Key="SelectionConverter" />
</Window.Resources>

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <ComboBox Grid.Row="0" ItemsSource="{Binding Coll1}" IsEnabled="{Binding SelectedPerson, Converter={StaticResource SelectionConverter}}" DisplayMemberPath="FirstName" Margin="6"/>
    <DataGrid Grid.Row="1" IsReadOnly="True" ItemsSource="{Binding Coll2}" SelectedItem="{Binding SelectedPerson}" Margin="6"/>

</Grid>
public class MainViewModel : INotifyPropertyChanged
{
    public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }

        public float Val { get; set; }

    }


    private object _selectedPerson;
    public object SelectedPerson
    {
        get { return _selectedPerson; }
        set
        {
            _selectedPerson = value;
            OnPropertyChanged("SelectedPerson");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged(string propertyName = null)
    {
        var handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }

    private bool _isItemSelected;
    public bool IsItemSelected
    {
        get { return _isItemSelected; }
        set
        {
            if (value == _isItemSelected) 
                return;
            _isItemSelected = value;
            OnPropertyChanged("IsItemSelected");
        }
    }


    private ObservableCollection<Person> _coll1;
    public ObservableCollection<Person> Coll1
    {
        get
        {
            return _coll1 ?? (_coll1 = new ObservableCollection<Person>());
        }
    }

    private ObservableCollection<Person> _coll2;
    public ObservableCollection<Person> Coll2
    {
        get
        {
            return _coll2 ?? (_coll2 = new ObservableCollection<Person>());
        }
    } 

    public MainViewModel()
    {
        Coll1.Add(
        new Person
        {
            FirstName = "TOUMI",
            LastName = "Redhouane",
            Val = 12.2f
        });

        Coll1.Add(
        new Person
        {
            FirstName = "CHERCHALI",
            LastName = "Karim",
            Val = 15.3f
        });

        Coll2.Add(
        new Person
        {
            FirstName = "TOUMI",
            LastName = "Djamel",
            Val = 12.2f
        });
        Coll2.Add(
        new Person
        {
            FirstName = "CHERCHALI",
            LastName = "Redha",
            Val = 12.2f
        });
    }


}
 public partial class MainWindow : Window
{

    public MainWindow()
    {
        InitializeComponent();
        DataContext = new MainViewModel();
    }
}
 public class SelectionConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {

        return value != null;
    }

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