C#WPF DataGrid绑定不适用于可观察的集合

时间:2018-10-16 11:43:55

标签: c# wpf mvvm binding datagrid

我有一个内部带有Channel Objects的Observable Collection。我试图将此Observable集合绑定到DataGrid,如下所示:

XAML:

<DataGrid Grid.Row="1" Grid.ColumnSpan="5" Height="45" Margin="30,10,30,255" ItemsSource="{Binding MGWChannels[0], Source={StaticResource MainWindowViewModel}, Mode=TwoWay}"
                                      AutoGenerateColumns="False" HorizontalAlignment="Stretch" CanUserAddRows="False" RowHeaderWidth="0" >
                                <DataGrid.Columns>
                                    <DataGridTextColumn Header="Max Retries" Width="auto" Binding="{Binding MGWChannels[0].MaxRetries, Source={StaticResource MainWindowViewModel}}" />
                                    <DataGridTextColumn Header="Backoff Time GW Unreachable" Width="auto" Binding="{Binding MGWChannels[0].BotwUnreachable, Source={StaticResource MainWindowViewModel}}"/>
                                    <DataGridTextColumn Header="Backoff Time GW Busy" Width="auto" Binding="{Binding MGWChannels[0].BotwMGWBusy, Source={StaticResource MainWindowViewModel}}"/>
                                    <DataGridTextColumn Header="Backoff Time GW Error" Width="auto" Binding="{Binding MGWChannels[0].BotwMGWError, Source={StaticResource MainWindowViewModel}}"/>
                                    <DataGridTextColumn Header="Strategy" Width="*"/>
                                </DataGrid.Columns>
                            </DataGrid>

ViewModel中的可观察集合:

private ObservableCollection<Channel> _mgwChannels;


public ObservableCollection<Channel> MGWChannels
{
    get
    {
        return this._mgwChannels;
    }
    set
    {
        this._mgwChannels = value;
        RaisePropertyChanged("MGWChannels");

    }
}

不幸的是,它没有按预期的方式工作-因为在Observable集合中有3个对象,所以我想制作3个DataGrid,并将它们与[0],[1]和[2]绑定。没有[0],它可以工作,但是将所有3个对象放入列表中,我只想为每个数据网格显示一个对象,而不是全部。

为什么不起作用?我错过了什么?

1 个答案:

答案 0 :(得分:0)

您的xaml应该类似于xaml。

  1. 不需要索引器,因为您需要将集合绑定到DataGrid。如果您只想显示一个对象,则必须创建另一个具有单个对象的集合。

  2. 在绑定属性时,无需查找ViewModel,因为这些属性应位于集合对象内。

    private ObservableCollection<ApplicationUser> firstMGWChannel;
    public ObservableCollection<ApplicationUser> FirstMGWChannel
    
    {
        get
        {
            return new ObservableCollection<ApplicationUser>() { MGWChannels[0] };
        }
    }
    

Xaml-

     <DataGrid Grid.Row="1" Grid.ColumnSpan="5" Height="45" Margin="30,10,30,255" ItemsSource="{Binding FirstMGWChannel, Source={StaticResource MainWindowViewModel}, Mode=TwoWay}"
                                  AutoGenerateColumns="False" HorizontalAlignment="Stretch" CanUserAddRows="False" RowHeaderWidth="0" >
        <DataGrid.Columns>
            <DataGridTextColumn Header="Max Retries" Width="auto" Binding="{Binding MaxRetries }" />
            <DataGridTextColumn Header="Backoff Time GW Unreachable" Width="auto" Binding="{Binding BotwUnreachable }"/>
            <DataGridTextColumn Header="Backoff Time GW Busy" Width="auto" Binding="{Binding BotwMGWBusy }"/>
            <DataGridTextColumn Header="Backoff Time GW Error" Width="auto" Binding="{Binding BotwMGWError }"/>
            <DataGridTextColumn Header="Strategy" Width="*"/>
        </DataGrid.Columns>
    </DataGrid>