How to obtain row index and assign it to observable collection property of datagrid

天涯浪子 提交于 2019-12-25 16:42:22

问题


Basically, I have a DataGrid inside a DataGrid:

<DataGrid ScrollViewer.CanContentScroll="True"
                          ScrollViewer.HorizontalScrollBarVisibility="Auto"
                          AutomationProperties.AutomationId="sCGridSelectedBasketItems"
                          HeadersVisibility="Row"
                          Height="{Binding ElementName=itemsgrid,Path=ActualHeight}"
                          Grid.Row="1"
                          RowDetailsVisibilityMode="Visible"
                          CanUserAddRows="false"
                          VerticalAlignment="Top"
                          AutoGenerateColumns="False"
                          SelectedItem="{Binding SelectedBasketItem}"
                          ItemsSource="{Binding SelectedOrderBasketItems}"
                          CanUserDeleteRows="True">
                    <DataGrid.RowDetailsTemplate>
                        <DataTemplate>
                            <DataGrid ScrollViewer.CanContentScroll="True"
                                      ScrollViewer.HorizontalScrollBarVisibility="Auto"
                                      HorizontalAlignment="Stretch"
                                      HorizontalContentAlignment="Stretch"
                                      CanUserAddRows="false"
                                      HeadersVisibility="Row"
                                      AutoGenerateColumns="False"
                                      ItemsSource="{Binding SelectedBasketItems}"
                                      SelectedValue="{Binding SelectedBasketItemValue}"
                                      SelectedIndex="{Binding SelectedOrderItemIndex}"
                                      SelectedItem="{Binding SelectedSpecificItemInBasket, Mode=TwoWay}"
                                      SelectionChanged="datagrid_SelectionChanged"     
                                      DataGrid.SelectionMode="Single"
                                      Style="{StaticResource GridItemsControlStyle}"
                                      ctrls:DragDrop.DropHandler="{Binding DropHandler}">

                                <DataGrid.Columns>
                                    <DataGridTextColumn IsReadOnly="True"
                                                        Binding="{Binding Path=ItemName}"
                                                        Width="195">
                                        <DataGridTextColumn.ElementStyle>
                                            <Style>
                                                <Setter Property="TextBlock.TextWrapping"
                                                        Value="Wrap" />
                                                <!--<Setter Property="TextBlock.TextAlignment"
                                                        Value="Center" />-->
                                            </Style>
                                        </DataGridTextColumn.ElementStyle>
                                    </DataGridTextColumn>

                                    <DataGridTemplateColumn>
                                        <DataGridTemplateColumn.CellTemplate>
                                            <DataTemplate>
                                                <Label Content="{Binding Path=RulesCount}"
                                                       Background="{Binding Path=RulesCount ,Converter={StaticResource ItemColourConverter}}" />
                                            </DataTemplate>
                                        </DataGridTemplateColumn.CellTemplate>
                                    </DataGridTemplateColumn>

                                    <DataGridTemplateColumn Width="115">
                                        <DataGridTemplateColumn.CellTemplate>
                                            <DataTemplate>
                                                <Label Content="{Binding Path=LinkedOrderCount}"
                                                       Background="{Binding Path=LinkedOrderCount ,Converter={StaticResource ItemColourConverter}}" />

                                            </DataTemplate>
                                        </DataGridTemplateColumn.CellTemplate>
                                    </DataGridTemplateColumn>

                                    <DataGridTemplateColumn Width="55">
                                        <DataGridTemplateColumn.CellTemplate>
                                            <DataTemplate>
                                                <Label Content="{Binding Path=SDICount}">
                                                    <Label.Background>
                                                        <MultiBinding Converter="{StaticResource SdiItemColourConverter}">
                                                            <Binding ElementName="SDIMaxCnt"
                                                                     Path="Value" />
                                                            <Binding Path="SDICount" />
                                                        </MultiBinding>
                                                    </Label.Background>
                                                </Label>
                                            </DataTemplate>
                                        </DataGridTemplateColumn.CellTemplate>
                                    </DataGridTemplateColumn>

                                </DataGrid.Columns>
                            </DataGrid>
                        </DataTemplate>
                    </DataGrid.RowDetailsTemplate>

My ObservableCollection has the following fields where I have to bind SelectedItemIndex of each DataGrid row with SelectedOrderItemIndex(which is a property of ObservableCollection class MobileOrderBasketItems.cs):

public int SelectedOrderItemIndex
    {
        get
        {
            return selectedOrderItemIndex;
        }

        set
        {
            selectedOrderItemIndex = value;
        }
    }

My Observable collection:

    public ObservableCollection<MobileOrderBasketItems> SelectedBasketItems
    {
        get
        {
            return _selectedBasketItems ?? (_selectedBasketItems = new ObservableCollection<MobileOrderBasketItems>());
        }
        set
        {
            _selectedBasketItems = value;
        }
    }

My ObservableCollection class definitions:

public class MobileOrderBasketItems
    {
        private int _sdiMaxCountVal;
        private int selectedOrderItemIndex;
        public string GrName { get; set; }
        public string ItemName { get; set; }
        public string RulesCount { get; set; }
        public string LinkedOrderCount { get; set; }
        public string SDICount { get; set; }

        public int SelectedOrderItemIndex
        {
            get
            {
                return selectedOrderItemIndex;
            }

            set
            {
                selectedOrderItemIndex = value;
            }
        }

        private ObservableCollection<MobileOrderBasketItems> _selectedBasketItems;
        public ObservableCollection<MobileOrderBasketItems> SelectedBasketItems
        {
            get
            {
                return _selectedBasketItems ?? (_selectedBasketItems = new ObservableCollection<MobileOrderBasketItems>());
            }
            set
            {
                _selectedBasketItems = value;
            }
        }
    }

Any help on how to achieve this is much appreciated.

来源:https://stackoverflow.com/questions/47790738/how-to-obtain-row-index-and-assign-it-to-observable-collection-property-of-datag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!