Dowemo
0 0 0 0


Question:

I feel like this should be easy, but I am stumped.

I am trying to bind a ComboBox.IsEnabled property to something like "Is an item currently selected on the DataGrid" property using MVVM. This way if no item is selected on the DataGrid, the ComboBox will be disabled.

Is there a DataGrid property that registers True/False when an item is selected, or do I need to do something with the SelectedItems.Count property?

I am trying to do this with as little code as possible before I write a converter or custom property.


Best Answer:


view

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

MainViewmodel:

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

MainWindow:

 public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = new MainViewModel();
    }
}

converter :

 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();
    }
}



Copyright © 2011 Dowemo All rights reserved.    Creative Commons   AboutUs