0

我有一个ComboBoxin ItemsControl。我使用 WPF 和 MVVM,我无法弄清楚绑定到ComboBox,有人会帮我解决这个问题吗?XAML 和 VM 如下:

    <Window x:Class="OutageManagement.Views.MarketAssignmentsView"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            mc:Ignorable="d"
            Title="Market Selection"
            WindowStartupLocation="CenterOwner"
            Width="700" Height="850"
            DataContext="{Binding MarketAssignmentsVM, Source={StaticResource Locator}}" >
      <Grid>
          <ItemsControl ItemsSource="{Binding USMarket}">
              <ItemsControl.ItemTemplate>
                 <DataTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition></ColumnDefinition>
                            <ColumnDefinition></ColumnDefinition>
                        </Grid.ColumnDefinitions>
                        <Label Content="{Binding MarketName}" Height="28" 
                            HorizontalAlignment="Left" Name="lblUSMarketName"  
                            VerticalAlignment="Center" />
                        <ComboBox Grid.Column="1" Height="23" HorizontalAlignment="Left" 
                              Name="cbUSUsers" VerticalAlignment="Center" MinWidth="140"
                              ItemsSource="{Binding RelativeSource={RelativeSource 
                              AncestorType=Window}, Path=UserList}" 
                              DisplayMemberPath="UserName" 
                              SelectedValue="{Binding SelectedUserID}" 
                              SelectedValuePath="UserID"/>
                     </Grid>
                 </DataTemplate>
             </ItemsControl.ItemTemplate>
         </ItemsControl>
      </Grid>
     </Window>

视图模型:

    public class MarketAssignmentsViewModel : ViewModelBase
    {


    #region Data
    ObservableCollection<NOCUserViewModel> _userList;
    ObservableCollection<MarketAssignmentViewModel> _usMarket;
    ObservableCollection<MarketAssignmentViewModel> _caMarket;
    #endregion 

    #region Constructor
    public MarketAssignmentsViewModel()
    {
        GetUserList();
        GetMarketAssignments();
    }
    #endregion 

    #region Properties

    public ObservableCollection<NOCUserViewModel> UserList
    {
        get { return _userList; }
    }
    public ObservableCollection<MarketAssignmentViewModel> USMarket
    {
        get { return _usMarket; }
    }

    public ObservableCollection<MarketAssignmentViewModel> CAMarket
    {
        get { return _caMarket; }
    }
    #endregion

    .
    .
    .
    }
4

1 回答 1

1

问题是您试图将UserListWindow 的属性作为 Window 的属性来访问,而不是 Window 的DataContext的属性...

修改ItemsSource如下:

<ComboBox ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor
                          AncestorType=Window}, Path=DataContext.UserList}" ... />

我建议您在遇到绑定问题时始终查看输出窗口,您可能会看到如下内容:

System.Windows.Data Error: 40 : BindingExpression path error: 'UserList' property not found on 'object' ''MarketAssignmentsView' (Name='')'.

于 2013-03-13T20:59:34.470 回答