- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
示例代码:
class GameListViewModel {
private IGameRepository repository;
public GameViewModel GameViewModel { get; set; }
public ObservableCollection<GameViewModel> Games { get; set; }
public ObservableCollection<GenreViewModel> Genres { get; set; }
public ICommand AddGame_ { get; set; }
public GameListViewModel() {
repository = new DummyGameRepository();
GameViewModel = new GameViewModel();
Games = new ObservableCollection<GameViewModel>(repository.GameList().Select(game => new GameViewModel(game)));
Genres = new ObservableCollection<GenreViewModel>(repository.GameList().Select(game => game.Genre).Distinct().Select(genre => new GenreViewModel(genre)));
AddGame_ = new RelayCommand(AddGame, CanAddGame);
}
}
class Game {
public string Title { get; set; }
public string SubTitle { get; set; }
public int Pegi { get; set; }
public Genre Genre { get; set; }
}
XAML:
<DataGrid ItemsSource="{Binding Games}" AutoGenerateColumns="False" Margin="0,32">
<DataGrid.Columns>
<DataGridTextColumn Header="Title" Binding="{Binding Title}" />
<DataGridTextColumn Header="Sub-Title" Binding="{Binding SubTitle}" />
<DataGridTextColumn Header="Pegi" Binding="{Binding Pegi}" />
<DataGridTextColumn Header="Genre" Binding="{Binding Genre.Name}" />
<DataGridComboBoxColumn Header="Test" ItemsSource="{Binding Genres}" DisplayMemberPath="Name" />
</DataGrid.Columns>
</DataGrid>
问题是,我希望组合框动态显示添加到游戏中的所有可能类型。为此,我在 GameListViewModel 中创建了一个 ObservableCollection。这是行不通的!我现在已经为此苦苦挣扎了 2 个小时......后来我希望所选的值成为游戏中的流派。
最佳答案
啊哈,您已经发现了许多 WPF 开发人员困惑的根源 - DataGrid 不会将它的 DataContext 转发到列。您不能真正依赖列中的 ItemsSource={Binding Genres}"之类的语句,因为框架无法解析它。这非常烦人。
此外,您还没有设置 SelectedItemBinding - 这是一个工作示例:
xaml:
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.Resources>
<CollectionViewSource Source="{Binding Possibilities}" x:Key="possibilities"/>
</Grid.Resources>
<DataGrid ItemsSource="{Binding Items}" AutoGenerateColumns="False" x:Name="dataGrid">
<DataGrid.Columns>
<DataGridComboBoxColumn Header="test"
SelectedItemBinding="{Binding Selection}"
ItemsSource="{Binding Source={StaticResource possibilities} }">
</DataGridComboBoxColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
CS 代码隐藏:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new ViewModel()
{
Items = new ObservableCollection<Item>()
{
new Item(){
Selection ="A"
}
}
};
}
}
public class Item : ViewModelBase
{
private string _selection = null;
public string Selection
{
get { return _selection; }
set { _selection = value; OnPropertyChanged("Selection"); }
}
}
public class ViewModel : ViewModelBase
{
private ObservableCollection<Item> _items = new ObservableCollection<Item>();
public ObservableCollection<Item> Items
{
get { return _items; }
set { _items = value; OnPropertyChanged("Items"); }
}
private ObservableCollection<string> _possibilities = new ObservableCollection<string>()
{
"A", "B", "C"
};
public ObservableCollection<string> Possibilities
{
get
{
return _possibilities;
}
set
{
_possibilities = value;
OnPropertyChanged("Possibilites");
}
}
}
public class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
var propChanged = PropertyChanged;
if (propChanged != null)
propChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
以下是一些有用的链接:这个应该工作: http://blogs.msdn.com/b/jaimer/archive/2008/11/22/forwarding-the-datagrid-s-datacontext-to-its-columns.aspx
这里显示的最后一个解决方案特别相关: http://blogs.msdn.com/b/vinsibal/archive/2008/12/17/wpf-datagrid-dynamically-updating-datagridcomboboxcolumn.aspx
关于c# - DataGridComboBoxColumn 不显示 ObservableCollection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27083769/
这是一个有趣的案例,我无法在网上找到任何信息。我正在尝试创建一个网格,需要将 ObservableCollection 的 ObservableCollection 绑定(bind)到它。想象这样一个
如何复制ObservableCollection项目到另一个 ObservableCollection没有引用第一个集合?这里ObservableCollection影响两个集合的项目值更改。 代码
public class Alpha { public ObservableCollection Items { get; set; } public Alpha() {
我只是想知道如何拥有父集合的子集合? 例如, 我已经有一个 ObservableCollection 产品,它正在添加并正确绑定(bind)到 XAML。但是,现在我需要另一个包含产品项目的 Obse
我对 Silverlight 体验相对较新,我正在尝试创建一个带有 DomainService 的 MVVM 应用程序,该应用程序将 POCO 作为模型返回。我有一个 UserControl,它有一个
查看 Microsoft 站点上的 Windows 运行时引用,我能找到的唯一相关集合是 IObservableVector 。 .NET Projection ObservableCollectio
我正在尝试获取值“thisValueIwant”。有没有可能如此容易地获得这个值(value)?或者也许这两个 ObservableCollection 有另一种解决方案 public class F
我有一个 ObserveableCollection,其中包含另一个 ObserveableCollection。在我的 WPF 中,我设置了对 Persons.Lectures 的绑定(bind)。
我有一个包含 20 个项目(图像)和按钮(“下一个”)的 observablecollection。我如何获得像 observablecollection.next() 和 observablecol
我有一个 ObservableCollection . T 有一个 ToString() 方法。我想做的是转换 ObservableCollection至 ObservableCollection .
我有一个 DataGrid,它绑定(bind)到 ViewModel 中的一个 ObservableCollection。这是一个搜索结果DataGrid。问题是,在我更新搜索结果 Observabl
有一堆ObservableCollection Result并要求将它们全部组合成另一个 ObservableCollection AllResults所以我可以在 listview 中显示它. 只是
哪个是保存我的数据的更好解决方案,还是取决于某些条件? 示例情况 1: 您需要显示一个数据列表,选择后可以在新窗口中修改。 示例情况 2: 您需要显示可以在此列表中修改的数据列表。 最佳答案 当您使用
从 xml 中执行 ViewModel 的最佳方法是什么:
所以我有一个 BaseClass 以及继承自基类的几个子类 ChildClass1 ChildClass2 我有ObservableCollections需要就地排序的子类,我无法创建新的 Obser
我为 ObservableCollection 构建了一个简单的扩展方法 AddRange: using System; using System.Collections.Generic; using
我的情况是,我有“tablegenerateModel”类的 ObservableCollection,该类进一步包含“column_Data”类的 ObservableCollection,并且这个
我将有一个 7 个小“购物 list ”,然后是一个包含 7 个小 list 中所有项目的大 list 。 是否可以使用 databind 和 observablecollection,以便从小列表中
当所述 ObservableCollection 从 View 模型公开时,我无法找到正确的绑定(bind)语法来绑定(bind) ObservableCollection 中包含的项目的属性。 当我
我有以下类(class),效果很好 public class RemoteSource { ObservableCollection remote; string[] _servers
我是一名优秀的程序员,十分优秀!