繁体   English   中英

WPF ComboBox SelectedItem 属性不起作用

[英]WPF ComboBox SelectedItem property is not working

我有这个组合框:

<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绑定不起作用 - 我总是得到列表中的第一个项目,而不是属于绑定到DataGrid项目的游戏数据的Mod对象。 如何显示正确的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;
        }
    }
}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM