我的WPF组合框中的绑定不起作用?

时间:2013-05-23 16:34:29

标签: c# .net wpf mvvm

我有一个ListView,它显示一个图像,两个文本框和一个组合框。 ListView绑定到视图模型中的集合。 Combobox绑定到视图模型中的另一个集合,所选项目绑定到列表视图中对象的属性。
预期结果:对于每个项目,组合框将显示当前图像的值。然后,用户应该能够在下拉列表中选择一个新值,并将其分配给图像对象。

我得到的:一个空白的组合框!当我点击v时,它显示所有没有显示名字的项目!

我的视图模型(它必须存在于我们当前应用程序设计的构造中)我为了简洁而删除了命令。 Items是绑定到ListView的集合,ArtifactTypes是ListView中为对象设置的项目列表。

    [AssociatedView(typeof(ManageImageView))]
public class ManageImagesViewModel : ClosableViewModelBase
{
    private Guid jobid;
    private TSObservableCollection<MobileImage> items = new TSObservableCollection<MobileImage>();
    private TSObservableCollection<ArtifactType> artifactTypes = new TSObservableCollection<ArtifactType>();

    public TSObservableCollection<MobileImage> Items
    {
        get
        {
            return items;
        }
        set
        {
            items = value;
        }
    }
    public TSObservableCollection<ArtifactType> ArtifactTypes
    {
        get
        {
            return artifactTypes;
        }
        set
        {
            if (artifactTypes != value)
                artifactTypes = value;
        }
    }

    public override string Title
    {
        get { return "Add and Delete Images"; }
    }

    public ManageImagesViewModel(IUnityContainer container)
        : base(container)
    {
        AddImage = new SimpleCommand(HandleAddImage);
        UpdateAll = new SimpleCommand(HandleUpdateAll);
        ExitRequest = new SimpleCommand(HandleExitProcess);
        DeleteCommand = new SimpleGenericCommand<Guid>(HandleDeleteProcess);
        List<ArtifactTypeDto> types = BusinessEngine.Mobile.GetTypeDefinitions();
        foreach (ArtifactTypeDto element in types)
        {
            ArtifactTypes.Add(new ArtifactType(element.Name, element.ArtifactTypeId));
        }
    }

我的观点定义:

            <ListView Width="510" MinHeight="600" ItemsSource="{Binding Items}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid Margin="4" Height="160" Width="Auto">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="160"/>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="50" />
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="42" />
                            <RowDefinition Height="25" />
                            <RowDefinition Height="25" />
                            <RowDefinition Height="25" />
                            <RowDefinition Height="42" />
                        </Grid.RowDefinitions>
                        <Image Source="{Binding Path=Image, Mode=OneWay}" Grid.Column="0" Grid.RowSpan="5" Margin="4" Height="150" Width="150" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                        <TextBox Grid.Column="1" Grid.Row="1" Text="{Binding Path=Name, Mode=TwoWay}" FontWeight="Bold" MinWidth="250" FontSize="12"/>
                        <TextBox Grid.Column="1" Grid.Row="2" Text="{Binding Path=Comment, Mode=TwoWay}" FontSize="12" />
                        <ComboBox Grid.Column="1" Grid.Row="3" ItemsSource="{Binding ArtifactTypes, Mode=OneTime}" DisplayMemberPath="Name" SelectedValuePath="Key" SelectedValue="{Binding Path=ArtifactTypeIdentity, Mode=TwoWay}" FontSize="12"/>
                        <Button Grid.Column="2" Grid.Row="1" Command="{Binding DeleteCommand}" CommandParameter="{Binding Path=ArtifactIdentity}">
                            <Button.Content>
                                <Image Margin="5,0,0,0" Height="24" Width="24" VerticalAlignment="Center" HorizontalAlignment="Center" Source="pack://application:,,,/SERVPRO.WorkCenter.Common.UI;component/Images/DeleteRed.png"/>
                            </Button.Content>
                        </Button>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListView>

最后 - 这里是填充组合框的值的定义:

    public class ArtifactType:INotifyPropertyChanged
{

    public ArtifactType(string name, Guid value)
    {
        this.Name = name;
        this.Key = value;
    }

    private string name;
    private Guid key;

    public string Name
    {
        get { return name; }
        set
        {
            if (name != value)
            {
                name = value;
                NotifyPropertyChanged("Name");
            }
        }
    }

    public Guid Key
    {
        get { return key; }
        set 
        {
            if (key != value)
            {
                key = value;
                NotifyPropertyChanged("Key");
            }
        }
    }

    private void NotifyPropertyChanged(string p)
    {
        if ( PropertyChanged != null )
            PropertyChanged(this, new PropertyChangedEventArgs(p));
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

所以有人在我的绑定中看到为什么我得到一个空白的组合框列表? (TSObservableCollection是我们的线程安全的ObservableCollection,用于处理更新集合时不在UI线程上,但会通知UI更改。)

1 个答案:

答案 0 :(得分:1)

如果在Visual Studio中运行时查看“输出”窗口,则应该看到存在绑定错误。

您拥有绑定集的方式,它正在寻找ArtifactTypes作为Items的属性,而不是ViewModel的属性,因为它是Items定义的ItemTemplate的一部分。

尝试这样的事情:

ItemsSource="{Binding DataContext.ArtifactTypes, Mode=OneTime, 
              RelativeSource={RelativeSource AncestorType={x:Type ListView}}}"

它的作用是将ItemsSource绑定到ListView的DataContext的ArtifactTypes属性。 DataContext是您的ViewModel。