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

property value changing back to null inside combobox of a datagrid column mvvm wpf project

$
0
0

Hi

I have a datagrid with two columns . First column has a combo box to choose a value from  and second column is a text box . Both of them display data correctly . This datagrid data collection is part of a model which is bound to tree view called myOrgTree.

DataGrid inside TreeView:

<DataGrid x:Name="myPropCollection" CanUserDeleteRows="True" Width="900"
                                  CanUserAddRows="False" CanUserResizeColumns="True" CanUserResizeRows="False"
                                   ItemsSource="{Binding ElementName=myOrgTree, Path=SelectedItem.OrgPropCollection,
                                                Mode=TwoWay}"
                                  SelectedItem="{Binding RelativeSource={RelativeSource FindAncestor,
                                  AncestorType={x:Type UserControl}}, Path=DataContext.SelectedproductNameValue}"
                                  AutoGenerateColumns="False"
                                  EnableRowVirtualization ="False"  Margin="0,2,0.333,-2.333"><DataGrid.RowHeaderStyle><Style TargetType="DataGridRowHeader"><Setter product="Width"><Setter.Value>20</Setter.Value></Setter><Setter product="Content"><Setter.Value><MultiBinding Converter="{StaticResource rowToIndexConverter}"><Binding /><Binding RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}" /></MultiBinding></Setter.Value></Setter></Style></DataGrid.RowHeaderStyle><DataGrid.Columns><!--product name coclumn--><DataGridTemplateColumn Header="product" Width="*"><DataGridTemplateColumn.CellEditingTemplate><DataTemplate><ComboBox
                                                     ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor,
                                                     AncestorType={x:Type UserControl}}, Path=DataContext.productNameCollection}"
                                                     Width="200"  x:Name="xProperties"
                                                     SelectedItem ="{Binding RelativeSource={RelativeSource FindAncestor,
                                                     AncestorType={x:Type Grid}}, Path=DataContext.productName}"
                                                     SelectedValue="{Binding productName}"><ComboBox.ItemTemplate><DataTemplate><TextBlock  Text="{Binding}" /></DataTemplate></ComboBox.ItemTemplate></ComboBox></DataTemplate></DataGridTemplateColumn.CellEditingTemplate><DataGridTemplateColumn.CellTemplate><DataTemplate><TextBlock Text="{Binding productName,Mode=TwoWay}"  ></TextBlock></DataTemplate></DataGridTemplateColumn.CellTemplate></DataGridTemplateColumn><!--value name coclumn--><DataGridTextColumn Header="Value" Width="*" Binding="{Binding productValue,Mode=TwoWay}" /></DataGrid.Columns></DataGrid>

my model which is bound to datagrid.OrgPRopCollection is an observable collection of  PRoductNameValueModel.                                   

    public class ProductNameValueModel: NodeBase
    {
        private string _ProductName;
        public string ProductName
        {
            get { return _ProductName; }

            set
            {
                if (value != null)
                {
                    _ProductName = value;
                    NotifyProductChanged("ProductName");
                }roperty

            }
        }


        private string _ProductValue;
        public string ProductValue
        {
            get { return _ProductValue; }

            set
            {
                _ProductValue = value;
                NotifyProductChanged("ProductValue");
            }
        }

    }//cls
while my datagrid displays data correctly , when i switch to another treeview item , the values in the previous/current treeViewitem's ProductName column are set to null which erases the values selected by user.  Whereas PRoductValue column (which is simply a textbox) could retain its value.To avoid this i have put a condition which is to check whether the ProductName value is not null. Since its just a workaround i would like to know how we can fix it in the first place. 


Krrishna




Viewing all articles
Browse latest Browse all 18858

Trending Articles