Sturm
Sturm

Reputation: 4125

Combobox selection change in XAML

I have a Combobox and two buttons in my UserControl. Is it possible to set those button to change the selected index of the Combobox directly in XAML?

I have done this by two approaches:

Code-behind

 private void nextBut_Click(object sender, RoutedEventArgs e)
        {
            combo.SelectedIndex++;
        }

        private void prevBut_Click(object sender, RoutedEventArgs e)
        {

            combo.SelectedIndex--;
        }

Or by binding commands to those buttons and define that command in my ModelView.

I have another question about XAML and I really don't know if ask a different question or use this opportunity that you are already reading me! I'm sure it has to be straightforward (at least for WPF gurus around here):

I have a ItemsControl that holds that UserControl, but there may be several or none (because you can create more, or delete). I want a Checkbox outside that is enabled or not depending if there are or not elements in my ItemsContol (disable if there is nothing). I think this can be done with Command Validation but looks difficult to me as I'm new in this world. This also could be done with codebehind but I would like to avoid it. (Like defining a bool property bound to that Checkbox, as write something like if(myItems.Count==0)

Upvotes: 1

Views: 1047

Answers (2)

Gayot Fow
Gayot Fow

Reputation: 8792

For the checkbox issue, it comes under the generic issue of converting a quantity into a bool. A canonical WPF answer would be bind the checkbox IsChecked property to the collection and route it through an IValueConverter. Here is a converter to do just that...

public class QuantityToBoolConverter: IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, 
                    System.Globalization.CultureInfo culture)
    {
        try
        {
            IEnumerable items = value as IEnumerable;
            if (items != null)
            {
                return items.OfType<object>().Any();
            }
        }
        catch
        {
            return value;
        }
    }
    public object ConvertBack(object value, Type targetType, object parameter, 
                        System.Globalization.CultureInfo culture)
    {
        return null;
    }
}

To deploy it, you would need to declare it in your Xaml as a resource...

<Window.Resources>
    <converters:QuantityToBoolConverter x:Key="QuantityToBoolConverter"/>
</Window.Resources>

And then bind the IsChecked property to your items and declare the converter...

<ListBox Name="mylb">
</ListBox>
<CheckBox IsChecked="{Binding ElementName=mylb, Path=ItemsSource, Converter={StaticResource 
                  QuantityToBoolConverter}}"></CheckBox>

For the combobox SelectedIndex issue, you can check out the CollectionViewSource docs. Here You can manipulate this in your ViewModel to move the current selection. And there's tons of sample code to examine. Knowing the ICollectionView will serve you well as a WPF developer.

Upvotes: 2

Fede
Fede

Reputation: 44038

I'd rather bind the SelectedItem property to some property in the ViewModel, and bind these buttons to some Commands in the ViewModel. This way keep the state data (selectedItem) in the ViewModel, and can use that to perform any additional logic required, removing the need for code behind.

For the CheckBox, I'd rather put a bool property in the ViewModel, and notify that whenever you add/remove items.

public bool HasItems {get {return Items.Any(); } }

public void AddItem()
{
    //...Add Items
    NotifyPropertyChanged("HasItems");
}

public void RemoveItem()
{
    //...Remove Item
    NotifyPropertyChanged("HasItems");
}

This removes the need for an additional converter.

Upvotes: 2

Related Questions