如何获取行索引并将其分配给datagrid的

时间:2017-12-13 10:27:31

标签: c# wpf mvvm observablecollection wpfdatagrid

基本上,我在DataGrid中有一个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具有以下字段,我必须将每个DataGrid行的SelectedItemIndex与SelectedOrderItemIndex(它是ObservableCollection类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;
        }
    }

我的ObservableCollection类定义:

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;
            }
        }
    }

非常感谢任何有关如何实现这一目标的帮助。

0 个答案:

没有答案