0

我之前读过很多文章来问这个问题,但最后我决定在这里写,因为我找不到适合我的解决方案的文章。

DataGrid在视图上有以下内容:

<DataGrid x:Name="UsersCollection" CanUserAddRows="False" CanUserDeleteRows="False" AutoGenerateColumns="False" HorizontalAlignment="Center" VerticalAlignment="Top" Height="525" Width="510" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Canvas.Left="147" Canvas.Top="26" Margin="0">
         <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Username, Mode=TwoWay, UpdateSourceTrigger=LostFocus}"/>
            <DataGridTextColumn Binding="{Binding Name, Mode=TwoWay, UpdateSourceTrigger=LostFocus}"/>
            <DataGridComboBoxColumn x:Name="Languages" />
         </DataGrid.Columns>

这是我的 ViewModel 的一部分:

 private List<string> _languages;



    [ImportingConstructor]
    public UsersViewModel()
    {
        DisplayName = "Users";
        _usersCollection = new TrulyObservableCollection<UserModel>();
        foreach (UserModel user in GetAllUsers())
            _usersCollection.Add(user);

        _usersCollection.CollectionChanged += Users_CollectionChanged;
        _usersCollection.ItemPropertyChanged += PropertyChangedHandler;

        _languages = new List<string>();
        _languages.Add("es");
        _languages.Add("ca");
        _languages.Add("en");
    }

    public List<string> Languages
    {
        get { return _languages; }
    }

我的 UserModel 有这个属性:

    public string Language
    {
        get { return _language; }
        set
        {
            if (_language != value)
            {
                _language = value;
                NotifyOfPropertyChange(() => Language);
            }
        }
    }

如何绑定DataGrid组合(当然,与 UserModel 的 Language 属性相关)?提前致谢!

4

2 回答 2

3

在您看来,请尝试以下操作:

<DataGrid x:Name="UsersCollection" CanUserAddRows="False" CanUserDeleteRows="False" AutoGenerateColumns="False" HorizontalAlignment="Center" VerticalAlignment="Top" Height="525" Width="510" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Canvas.Left="147" Canvas.Top="26" Margin="0">
     <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding Username, Mode=TwoWay, UpdateSourceTrigger=LostFocus}"/>
        <DataGridTextColumn Binding="{Binding Name, Mode=TwoWay, UpdateSourceTrigger=LostFocus}"/>
        <DataGridComboBoxColumn ItemsSource={Binding ElementName=UsersCollection, Path=DataContext.Languages} SelectedItemBinding={Binding Path=Language} />
     </DataGrid.Columns>

如果您拥有DataGrid内部的视图具有名称,例如,如果您DataGrid位于UserControl具有名称的 a 内部myUserControl,则尝试以下小的修改:

<DataGridComboBoxColumn ItemsSource={Binding ElementName=myUserControl, Path=DataContext.Languages} SelectedItemBinding={Binding Path=Language} />
     </DataGrid.Columns>

如果包含 的视图DataGrid没有名称,只需给它一个名称并使用上面的代码。

于 2013-10-25T10:53:44.793 回答
1

我设法运行了一些东西:

<DataGrid x:Name="UsersCollection" CanUserAddRows="False" CanUserDeleteRows="False" AutoGenerateColumns="False" HorizontalAlignment="Center" VerticalAlignment="Top" Height="525" Width="510" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Canvas.Left="147" Canvas.Top="26" Margin="0">
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding Username, Mode=TwoWay, UpdateSourceTrigger=LostFocus}"/>
        <DataGridTextColumn Binding="{Binding Name, Mode=TwoWay, UpdateSourceTrigger=LostFocus}"/>
        <DataGridComboBoxColumn Width="*">
            <DataGridComboBoxColumn.ElementStyle>
            <Style TargetType="ComboBox">
                <Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.Languages}"/>
                <Setter Property="SelectedItem" Value="{Binding Language}" />
            </Style>
        </DataGridComboBoxColumn.ElementStyle>
        <DataGridComboBoxColumn.EditingElementStyle>
            <Style TargetType="ComboBox">
                <Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.Languages}"/>
                <Setter Property="SelectedItem" Value="{Binding Language}" />
            </Style>
        </DataGridComboBoxColumn.EditingElementStyle>
        </DataGridComboBoxColumn>
    </DataGrid.Columns>
</DataGrid>

实际上对我有用。

于 2013-11-04T10:20:35.757 回答