- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
问题范围于 2010-03-25 扩大
我最终弄清楚了我的问题,但是由于解决了原始问题,所以出现了一个新问题,因为我希望能够将赏金奖励给某人!!!
一旦我发现了我的问题,我很快就发现当 ObservableCollection 更新时,数据绑定(bind)的 ComboBox 重新填充了它的内容,但大多数选择都被清除了。
我假设在这种情况下,MVVM 会让我难以记住最后选择的项目。我有一个想法,但它似乎有点讨厌。我会将赏金奖励给为此提出一个很好的解决方案的人!
问题重写于 2010-03-24
我有两个用户控件,其中一个是具有 TabControl 的对话框,另一个是出现在所述 TabControl 中的对话框。我就叫他们糖果对话 和 CandyNameViewer 为了简单起见。还有一个名为 的数据管理类。追踪器 它管理信息存储,出于所有意图和目的,它只公开一个公共(public)属性,即 ObservableCollection。
我通过后面的代码在 CandyDialog 中显示 CandyNameViewer,如下所示:
private void CandyDialog_Loaded( object sender, RoutedEventArgs e)
{
_candyviewer = new CandyViewer();
_candyviewer.DataContext = _tracker;
candy_tab.Content = _candyviewer;
}
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Page.Resources>
<DataTemplate x:Key="CandyItemTemplate">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="120"></ColumnDefinition>
<ColumnDefinition Width="150"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBox Grid.Column="0" Text="{Binding CandyName}" Margin="3"></TextBox>
<!-- just binding to DataContext ends up using InventoryItem as parent, so we need to get to the UserControl -->
<ComboBox Grid.Column="1" SelectedItem="{Binding SelectedCandy, Mode=TwoWay}" ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.CandyNames}" Margin="3"></ComboBox>
</Grid>
</DataTemplate>
</Page.Resources>
<Grid>
<ListBox DockPanel.Dock="Top" ItemsSource="{Binding CandyBoxContents, Mode=TwoWay}" ItemTemplate="{StaticResource CandyItemTemplate}" />
</Grid>
</Page>
最佳答案
当 DataContext 更改时,ComboBoxes 出现空白时,我遇到了同样的问题。我有一个相对简单的解决方案,它使用我编写的名为“ComboBoxFixer”的类。一旦实现,您可以通过简单地替换这个来解决这个问题:
<ComboBox ItemsSource="..." SelectedItem="..." />
<ComboBox ItemsSource="..." my:ComboBoxFixer.SelectedItem="..." />
public class DependencyPropertySynchronizer
{
public DispatcherPriority Priority { get; set; }
public DependencyProperty AutoSyncProperty { get; set; }
public DependencyProperty Register(...
public DependencyProperty RegisterAttached(...
public DependencyPropertyKey RegisterReadOnly(...
public DependencyPropertyKey RegisterAttachedReadOnly(...
}
public SomeClass : DependencyObject
{
static DependencyPropertySynchronizer sync =
new DependencyPropertySynchronizer
{
Priority = DispatcherPriority.ApplicationIdle
};
public static readonly DependencyProperty HappinessProperty =
sync.RegisterAttached("Happiness", typeof(int), typeof(SomeClass));
public static readonly DependencyProperty JoyProperty =
sync.RegisterAttached("Joy", typeof(int), typeof(SomeClass));
}
public class ComboBoxFixer : DependencyObject
{
static DependencyPropertySynchronizer sync =
new DependencyPropertySynchronizer
{
Priority = DispatcherPriority.ApplicationIdle,
AutoSyncProperty = Selector.SelectedItemProperty,
};
public static readonly DependencyProperty SelectedItemProperty =
sync.RegisterAttached("SelectedItem", typeof(object), typeof(ComboBoxFixer),
new FrameworkPropertyMetadata
{
BindsTwoWayByDefault = true,
});
public static object GetSelectedItem(... // normal attached property stuff
public static void SetSelectedItem(...
}
ViewModel property
<- bound from ->
my:ComboBoxFixer.SelectedItem
<- synced with ->
ComboBox.SelectedItem
关于c# - 数据绑定(bind)到不同 UserControl 中的 ObservableCollection - 如何保留当前选择?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2480450/
这是一个有趣的案例,我无法在网上找到任何信息。我正在尝试创建一个网格,需要将 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
我是一名优秀的程序员,十分优秀!