我在这个链接上找到了
ObservableCollection没有注意到它中的Item何时发生变化(即使使用INotifyPropertyChanged)
一些通知Observablecollection项目已更改的技术.这个链接中的TrulyObservableCollection似乎正是我正在寻找的.
public class TrulyObservableCollection<T> : ObservableCollection<T>
where T : INotifyPropertyChanged
{
public TrulyObservableCollection()
: base()
{
CollectionChanged += new NotifyCollectionChangedEventHandler(TrulyObservableCollection_CollectionChanged);
}
void TrulyObservableCollection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.NewItems != null)
{
foreach (Object item in e.NewItems)
{
(item as INotifyPropertyChanged).PropertyChanged += new PropertyChangedEventHandler(item_PropertyChanged);
}
}
if (e.OldItems != null)
{
foreach (Object item in e.OldItems)
{
(item as INotifyPropertyChanged).PropertyChanged -= new PropertyChangedEventHandler(item_PropertyChanged);
}
}
}
void item_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
NotifyCollectionChangedEventArgs a = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset);
OnCollectionChanged(a); …Run Code Online (Sandbox Code Playgroud) c# collections wpf observablecollection inotifypropertychanged
我正在尝试将一个集合绑定到ItemsControl,将Canvas作为items面板,并将每个项目的Canvas.Left和Top绑定到项目对象上的属性.基本上我正在尝试重新创建我在博客上的这篇文章中描述的二维数据绑定,但这次是在WinRT而不是WPF.
由于ItemsControl将您的ItemTemplate内容包装在另一个UI元素(在WinRT的情况下为ContentPresenter)中,并且它是直接放置在items面板中的那些包装器/容器元素,因此必须在这些容器上设置Left和Top; 你不能只在DataTemplate中设置它们.在WPF中,使用ItemContainerStyle中的绑定很容易做到这一点,例如:
<ItemsControl.ItemContainerStyle>
<Style>
<Setter Property="Canvas.Left" Value="{Binding Path=X}"/>
<Setter Property="Canvas.Top" Value="{Binding Path=Y}"/>
</Style>
</ItemsControl.ItemContainerStyle>
Run Code Online (Sandbox Code Playgroud)
但是当我在WinRT/XAML项目中尝试同样的事情时,我什么也得不到.甚至没有绑定错误.如果我对一个值进行硬编码,它就会起作用; 但是如果我使用绑定,则该属性将保持其默认值(零),并且"输出"窗口中不显示任何绑定错误.
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<!-- This works, so ItemContainerStyle does work in WinRT: -->
<Setter Property="Canvas.Left" Value="200"/>
<!-- But this silently fails, leaves Top as 0, and does not show
any binding errors in the debugger's Output window: -->
<Setter Property="Canvas.Top" Value="{Binding Y}"/>
</Style>
</ItemsControl.ItemContainerStyle>
Run Code Online (Sandbox Code Playgroud)
我已经验证了ContentPresenters确实有正确的DataContext(即集合项,而不是集合本身或其他时髦的东西),所以你认为这些绑定可以正常工作.但他们似乎甚至没有得到评估.如果我在其他地方放置了一个错误的绑定,并运行调试版本,我会在调试器的Output窗口中看到绑定错误; 但是如果我在ItemContainerStyle中引用了一个无意义的属性,则不会显示绑定错误.
这是一个更完整的例子,(据我所知)应该可以在WPF中正常工作,但是在WinRT中,所有内容都保留在原点:
<ItemsControl ItemsSource="{Binding Tiles}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style …Run Code Online (Sandbox Code Playgroud) binding itemscontrol itemcontainerstyle windows-runtime winrt-xaml
我有一个WPF ListBox控件,我将其设置ItemsSource为项目对象的集合.如何将对象的IsSelected属性绑定ListBoxItem到Selected相应项对象的属性,而不将对象的实例设置为Binding.Source?
我正在尝试将所选的默认浅灰色突出显示更改为聚焦ListViewItem时显示的蓝色突出显示ListView.我一直在努力在线调和不同的StackOverflow答案和来源,但我还没想出我还需要什么样的XAML.我有以下内容:
<ListView ItemContainerStyle="{StaticResource checkableListViewItem}"
SelectionMode="Multiple"
View="{StaticResource fieldValueGridView}"/>
Run Code Online (Sandbox Code Playgroud)
引用的View资源是:
<GridView x:Key="fieldValueGridView" AllowsColumnReorder="False">
<GridViewColumn Header="Field">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock FontWeight="Bold" Text="{Binding Path=DisplayName}"/>
<TextBlock FontWeight="Bold" Text=": "/>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Value" DisplayMemberBinding="{Binding Path=FieldValue}"/>
</GridView>
Run Code Online (Sandbox Code Playgroud)
引用的ItemContainerStyle资源是:
<Style TargetType="ListViewItem" BasedOn="{StaticResource {x:Type ListViewItem}}"
x:Key="checkableListViewItem">
<Setter Property="IsSelected" Value="{Binding Path=IsChecked}" />
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="VerticalContentAlignment" Value="Top" />
</Style>
Run Code Online (Sandbox Code Playgroud)
的ListView的IsEnabled属性不会改变,如果该事项.我想以某种方式合并<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="{x:Static SystemColors.HighlightColor}"/>,或类似的东西,以突出显示ListViewItem未聚焦的选定的ListView.
我有以下风格,但这似乎并没有影响ListViewItem …
wpf ×3
binding ×1
c# ×1
collections ×1
data-binding ×1
gridview ×1
itemscontrol ×1
listboxitem ×1
listview ×1
selected ×1
templates ×1
winrt-xaml ×1
xaml ×1