Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

WPF DataGrid Combo Box binding issue in 4.0 framework

$
0
0

Hi,

I am facing a problem with DataGrid Combo box binding. My requirement is 

1.DataGrid to have combo box enabled Column and bind with dictionary. User can select cell value from drop down.

2.The same column to have customized header having a label and combo box. Purpose of header combo box is to act as filter for the column. Both combo box (header and at cell level) to have same data source

I have written the code and its working fine on my system. But when I migrate the build to other system, I could not see the data in drop down for cell combo box. In header I could see it.

Difference between two systems is, mine has VS2013 installed and other does not. I guess its a framework issue, cause when i install v4.5 framework on other system, it started working.

My Question is, is there any way, through coding, I could overcome this issue and I don't force others to install v4.5 on their systems.

I created POC replicating the same issue.

 public partial class MainWindow : Window {

        public int ValueTypeSelected { get; set; }
        public ObservableCollection<int> list;
        private CollectionViewSource collectionList;

        public Dictionary<int, string> ValueType
        {
            get
            {
                var list = new Dictionary<int, string>();
                list.Add(1,"High");
                list.Add(2, "Low");

                return list;
            }
        }

        public MainWindow() {
            InitializeComponent();

            list = new ObservableCollection<int>();
            collectionList = new CollectionViewSource() { Source = list };
            MainGrid.DataContext = collectionList.View;
            list.Add(1);
            list.Add(2);
            list.Add(1);
        }
    }

XMAL copy of datagrid:

<DataGrid Name="MainGrid" ItemsSource="{Binding}" HorizontalAlignment="Left" Margin="41,58,0,0" Height="132" Width="303" AutoGenerateColumns="False">
            <DataGrid.Columns>
            <DataGridTemplateColumn Header="Value Type" MaxWidth="80">
                <DataGridTemplateColumn.HeaderStyle>
                    <Style TargetType="DataGridColumnHeader">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate>
                                    <StackPanel HorizontalAlignment="Stretch">
                                        <Label Content="{Binding}"/>
                                        <ComboBox Name="btnFilter" ItemsSource="{BindingElementName=Main, Path=ValueType}" SelectedValuePath="Key" DisplayMemberPath="Value"IsEditable="True"/>
                                    </StackPanel>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </DataGridTemplateColumn.HeaderStyle>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                            <TextBlock Text="{Binding ElementName =Main,Path=ValueTypeSelected}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
                <DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>
                            <ComboBox ItemsSource="{Binding ElementName=Main, Path=ValueType}"SelectedValuePath="Key" DisplayMemberPath="Value" 
                                     SelectedValue="{Binding ElementName =Main,Path=ValueTypeSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellEditingTemplate>
            </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>


Any help will be appreciated.

Thanks.



Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>