[英]Blend/XAML: bind ItemsSource via button
I am creating a Windows 8.1 app and have the following code: 我正在创建Windows 8.1应用程序,并具有以下代码:
<ListView x:Name="listView" Height="505" Canvas.Left="35" Canvas.Top="75" Width="300" ItemTemplate="{StaticResource GroupTemplate}" ItemsSource="{Binding Groups, Mode=TwoWay, UpdateSourceTrigger=Explicit}" HorizontalAlignment="Left" VerticalAlignment="Top" UseLayoutRounding="False" ScrollViewer.VerticalScrollBarVisibility="Disabled" SelectionMode="None" />
<Button Height="40" Width="260" HorizontalAlignment="Left" VerticalAlignment="Top" Canvas.Left="40" Canvas.Top="585" BorderThickness="0" Content="Overige tonen" Background="#FF9B9B9B" Style="{StaticResource ButtonStyle1}" Click="show_items2" />
When the user clicks the show_items2
button, the ItemsSource
Groups should be replaced by another ItemsSource
. 当用户单击
show_items2
按钮时, ItemsSource
组应替换为另一个ItemsSource
。 I use sampledata 'Groups' from Blend for Visual Studio 2013. I have the pretty obvious code for the button, shown below: 我使用Blend for Visual Studio 2013的sampledata'Groups'。按钮的代码非常明显,如下所示:
private void show_Medicatie2(object sender, RoutedEventArgs e)
{
// replace itemsSource with another
}
I've tried many tutorials but nothing seams to work. 我已经尝试了许多教程,但是没有接缝可以工作。 Do I miss something?
我想念什么吗? Help is really appreciated.
非常感谢您的帮助。 Thank you!
谢谢!
更改Groups
时需要RaisePropertyChanged("Groups")
The class which you are binding to (in your case, the owner of Groups
) is required to be an INotifyPropertyChanged
so that you can explicitly tell the WPF that the binding needs refreshing. 绑定到的类(在您的情况下,为
Groups
的所有者)必须为INotifyPropertyChanged
以便您可以明确告知 WPF绑定需要刷新。 This is a requirement if you are not using DependencyProperties
. 如果您不使用
DependencyProperties
则这是一项要求。
Add this to your class definition: 将此添加到您的类定义:
class X : System.ComponentModel.INotifyPropertyChanged
{
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
public IEnumerable<object> Groups //Replace 'object' with the type your are using...
{
get{ return m_Groups; }
set
{
m_Groups = value;
//Raise the event to notify that the property has actually got a new value
var handler = PropertyChanged;
if(handler != null)
PropertyChanged(this, new PropertyChangedEventArgs("Groups"));
}
} IEnumerable<object> m_Groups = new List<object>();
}
Why does this work? 为什么这样做?
WPF knows about INotifyPropertyChanged
and when you use an object as a Binding source, WPF checks if it implements the interface. WPF知道
INotifyPropertyChanged
并且在将对象用作绑定源时,WPF会检查它是否实现了接口。 If it does, WPF subscribes to the event. 如果是这样,则WPF订阅该事件。 When the event is triggered, the property name is checked and if it matches the last property in the binding path , the binding is updated.
触发事件时,将检查属性名称,如果它与绑定路径中的最后一个属性匹配,则将更新绑定。
You can also replace IEnumerable<T>
with ObservableCollection<T>
but things start getting tricky soon afterwards. 您也可以用
ObservableCollection<T>
替换IEnumerable<T>
,但是之后很快就会变得棘手。 I recommended reading up about ObservableCollection<T>
! 我建议阅读有关
ObservableCollection<T>
!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.