c# - 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;
}
}
}
解决方案
您应该INotifyPropertyChanged
在 ViewModel 中实现以确保SelectedItem
Mod在 UI 线程中更新
推荐阅读
- android - 如何了解锂离子或其他电池技术?
- c++ - 数组、对象、类来组织具有多项选择和真/假问题的测试问卷程序
- python - 在python中对数据框中的列求和
- apache-kafka - 如何从 KSQL 中的单个主题创建不同的表?
- windows - 在 windows openvpn 服务器上设置身份验证用户密码
- windows - 是否可以通过 pgAgent 在 Windows 中运行 .exe?
- installation - Install Shield Silent 响应文件(.iss 文件)是否依赖于登录用户权限
- node.js - apache2上socket.io和nodejs的CORS头错误
- typescript - 从对象动态创建类型
- node.js - 如果“闰秒被“涂抹”以致不需要闰秒表,这意味着什么?