首页 > 解决方案 > WPF ComboBox SelectedItem 属性不起作用

问题描述

我有这个组合框:

<DataGridTemplateColumn SortMemberPath="Mod.Code" Header="Mod" Width="100*">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <ComboBox ItemsSource="{Binding Mode=OneWay, Source={StaticResource modViewSource}}" SelectedItem="{Binding Mod}" IsSynchronizedWithCurrentItem="True"/>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

这是数据源:

<CollectionViewSource x:Key="modViewSource" d:DesignSource="{d:DesignInstance {x:Type AutoPBW:Mod}, CreateList=True}">
    <CollectionViewSource.SortDescriptions>
        <scm:SortDescription PropertyName="Code"/>
    </CollectionViewSource.SortDescriptions>
</CollectionViewSource>

这是模型属性:

/// <summary>
/// The mod used for this game.
/// </summary>
public Mod Mod { get; set; }

我的问题是SelectedItem绑定不起作用 - 我总是得到列表中的第一项,而不是Mod属于绑定到DataGrid. 我怎样才能显示正确Mod

编辑:这里是 Mod.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;

namespace AutoPBW
{
    /// <summary>
    /// A game mod, or the stock game.
    /// </summary>
    public class Mod
    {
        public Mod(string code, string engineCode)
        {
            Code = code;
            Engine = Engine.Find(engineCode);
            IsUnknown = true;
        }

        /// <summary>
        /// A unique code name for this mod. Used in PBW URLs.
        /// </summary>
        public string Code { get; set; }

        public override string ToString()
        {
            return Code;
        }

        /// <summary>
        /// The game engine that this mod uses.
        /// </summary>
        public Engine Engine { get; set; }

        /// <summary>
        /// The path to the mod's root directory, relative to the engine's root directory.
        /// </summary>
        public string Path { get; set; }

        /// <summary>
        /// The path to the mod's savegame directory, relative to the engine's root directory.
        /// </summary>
        public string SavePath { get; set; }

        /// <summary>
        /// The path to the mod's empire setup directory, relative to the engine's root directory.
        /// </summary>
        public string EmpirePath { get; set; }

        /// <summary>
        /// Is this an unknown mod?
        /// </summary>
        public bool IsUnknown { get; set; }

        public static Mod Find(string code, string defaultEngineCode = null)
        {
            if (code == null)
                return null;
            var old = Config.Instance.Mods.SingleOrDefault(x => x.Code == code);
            Mod nu;
            if (old == null || old.IsUnknown)
            {
                // load from default if present
                var d = Config.Default.Mods.SingleOrDefault(x => x.Code == code);
                if (d != null)
                    nu = d;
                else
                    nu = new Mod(code, defaultEngineCode); // let the user know what the code is so he can find the mod
            }
            else
                nu = old;
            if (nu != old)
            {
                if (old != null)
                    Config.Instance.Mods.Remove(old);
                Config.Instance.Mods.Add(nu);
            }
            return nu;
        }
    }
}

标签: c#wpfxaml

解决方案


您应该INotifyPropertyChanged在 ViewModel 中实现以确保SelectedItem Mod在 UI 线程中更新


推荐阅读