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

本文关键字:绑定 不起作用 WPF 组合 我的 | 更新日期: 2023-09-27 18:15:16

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

结果是:一个空白的组合框!当我点击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;
}

那么有人看到我的绑定为什么我得到一个空白的组合框列表吗?(tsoobservablecollection是线程安全的ObservableCollection,用于处理当不在UI线程上但UI被通知更改时更新集合)

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

如果您在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