c# - 我的 WPF 组合框中的绑定(bind)不起作用?

标签 c# .net wpf mvvm

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

我得到的是:一个空白的组合框!当我单击 v 时,它会显示所有未显示名称的项目!

我的 View 模型(它必须存在于我们当前应用程序设计的结构中)为简洁起见,我删除了这些命令。 Items 是绑定(bind)到 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));
        }
    }

我的 View 定义:
            <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;
}

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

最佳答案

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

您设置绑定(bind)的方式是寻找 ArtifactTypes 作为 Items 的属性,而不是您的 ViewModel,因为它是 Items 定义的 ItemTemplate 的一部分。

尝试这样的事情:

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

这样做是将 ItemsSource 绑定(bind)到 ListView 的 DataContext 的 ArtifactTypes 属性。该 DataContext 是您的 ViewModel。

关于c# - 我的 WPF 组合框中的绑定(bind)不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16719335/

相关文章:

c# - 本地化的 System.Windows.Media.Color 名称

c# - razor 页面上的 Dropzone 返回 400 状态代码

c# - 有没有一种使用 ASP.NET 验证器比较复选框和文本框的优雅方法?

.net - 我应该转储所有 WebForms/ASP.NET MVC 和 WinForms 开发并转移到 Silverlight 吗?

c# - SelectionChanged 显示旧值

wpf - 如何在不使用代码的情况下处理 WPF Datagrid 双击

c# - 如何使用c#从excel文件中读取数据

c# - SHA256CryptoServiceProvider 不符合 FIPS 标准?

c# - 为什么我不能使用 Entity Framework 保存当前的 DateTime.Now

wpf - 在 XAML 中实例化和重用对象的实例