1

Following the answers in these 2:

Using WPF DataGridComboBoxColumn with MVVM - Binding to Property in ViewModel

Binding a WPF DataGridComboBoxColumn with MVVM

1) I can't get the values in the ObservableCollections to be set when a selection in the combobox is made.

The comboboxes are being populated with the List from the ViewModel but values are not being set.

Code:

<DataGrid ItemsSource="{Binding SidValues1through5, Mode=TwoWay}"                                  
     AutoGenerateColumns="False"
     Grid.Row="1"   
     Margin="5"
     VerticalAlignment="Top"
     HorizontalAlignment="Center">
     <DataGrid.Columns>
           <DataGridComboBoxColumn Header="1"
                                   Width="100"
                                   SelectedValueBinding="{Binding Value1}"
                                   SelectedValuePath="Value1">
                                    <DataGridComboBoxColumn.ElementStyle>
                                        <Style TargetType="ComboBox">
                                            <Setter Property="ItemsSource"
                                                    Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.AvailableSids}" />
                                        </Style>
                                    </DataGridComboBoxColumn.ElementStyle>
                                    <DataGridComboBoxColumn.EditingElementStyle>
                                        <Style TargetType="ComboBox">
                                            <Setter Property="ItemsSource"
                                                    Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.AvailableSids}" />
                                        </Style>
                                    </DataGridComboBoxColumn.EditingElementStyle>
                                </DataGridComboBoxColumn>

ViewModel interface (I've debugged and I am connected to the ViewModel, other controls on the view are bound correctly:

ETA: BindableCollection inherits from ObservableCollection it's a Caliburn.Micro type.

public interface ICustomSIDViewModel : IScreen
{
    BindableCollection<SidValues> SidValues1through5 { get; set; }
    BindableCollection<SidValues> SidValues6through10 { get; set; }

    IList<string> AvailableSids { get; }
}

public class SidValues
{
    public string Value1 { get; set; }
    public string Value2 { get; set; }
    public string Value3 { get; set; }
    public string Value4 { get; set; }
    public string Value5 { get; set; }
}

2) Once I resolve this is there a cleaner way to have all the columns inherit this one set of DataGridComboBoxColumn.ElementStyle and DataGridComboBoxColumn.EditingElementStyle?

Reason I ask is there are 10 columns all will have the same combobox list.

Community
  • 1
  • 1
Mark W
  • 3,879
  • 2
  • 37
  • 53

3 Answers3

0
SelectedValueBinding="{Binding SelectedValue, Mode=TwoWay}"
                                       SelectedValuePath="Value1">

private string selectedValue;
    public string SelectedValue 
    {
        get
        {
            return selectedValue;
        }
        set
        {
            selectedValue = value;
            Notify("SelectedValue");
        } 
    }

    private void Notify(string propName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propName));
    }

You are binding one of the property of Collection that is your ComboBox's ItemsSource ,thats wrong .It should be like you should have separate Property as SelectedValue above and bind this Property to the SelectedValue of ComboBox.And In this Property You can get and set the Selected value of the ComboBox.I hope this will help.

yo chauhan
  • 12,079
  • 4
  • 39
  • 58
  • error in output window while running: System.Windows.Data Error: 40 : BindingExpression path error: 'Value1' property not found on 'object' ''String' (HashCode=-1241299750)'. BindingExpression:Path=Value1; DataItem='String' (HashCode=-1241299750); target element is 'TextBlockComboBox' (Name=''); target property is 'NoTarget' (type 'Object') – Mark W Aug 10 '12 at 20:02
  • I'm having a hard time getting this to work myself - I do the same thing with Teleriks RadGrid control and it works fine.. but setting the ItemsSource on a DataGrid doesn't seem to work for me! – Charleh Aug 11 '12 at 13:58
  • Instead of IList try ObservableCollection – yo chauhan Aug 11 '12 at 18:30
0

For the first question - it's WPF so you shouldn't need to use Mode=TwoWay on your bindings, but try that for starters just in case.

The default for WPF afaik is TwoWay, but not for SL. Try it anyway.

For the second question, just declare a nested style in your resource dictionary. Nested styles apply to child elements of the target control

e.g.

<Style x:Key="DataGridComboBoxStyle" TargetType="DataGrid">
    <!-- Nested -->
    <Style.Resources>
            <Style TargetType="ComboBox">
                <Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.AvailableSids}" />
            </Style>
    </Style.Resources>
</Style>

You could also apply a style to the entire control and nest this style in that style :)

Charleh
  • 13,749
  • 3
  • 37
  • 57
  • If I add that whole block into the resources the ComboBox portion is greyed out which means not being used. If I put just the Style TargetType="ComboBox" portion in the resources section at the top of the xaml and set the Style BasedOn="{StaticResource ResourceKey=ComboBoxStle}" (I named the style with an x:Key="ComboBoxStyle" I get the following error: – Mark W Aug 10 '12 at 17:31
  • "Can only base on a Style with target type that is base type 'IFrameworkInputElement'." – Mark W Aug 10 '12 at 17:33
  • No that should work - nested styles definitely work - hang on I'll check this on mine maybe syntax is a little off – Charleh Aug 10 '12 at 17:42
  • what do I do with the DataGridComboBoxColumn.ElementStyle and DataGridComboBoxColumn.EditingElementStyle sections? They require something inside. – Mark W Aug 10 '12 at 20:05
0

What I finally had to go with to work and move on with the project was a ListView with a GridView inside. Not exactly the same but looks similar.

I'm still curious on how to get a DataGrid to actually work with MVVM and Caliburn.Micro, I tried every example I found and couldn't get the combobox selection to update anything on the VM.

Here's my solution:

<ListView.View>
    <GridView>                                    
        <GridViewColumn Header="1"
                       Width="100">
            <GridViewColumn.CellTemplate>
                <DataTemplate>
                    <ComboBox ItemsSource="{Binding Path=DataContext.AvailableSids,
                                        RelativeSource={RelativeSource FindAncestor, 
                                        AncestorType={x:Type UserControl}}}"
                              SelectedItem="{Binding Path=Value1, Mode=TwoWay, 
                                              UpdateSourceTrigger=PropertyChanged}" />
                </DataTemplate>
            </GridViewColumn.CellTemplate>
        </GridViewColumn>
    </GridView>  
</ListView.View>
Ric .Net
  • 5,540
  • 1
  • 20
  • 39
Mark W
  • 3,879
  • 2
  • 37
  • 53