wpff MVVM中的选定项绑定 [英] Selected item binding in wpff MVVM

查看:79
本文介绍了wpff MVVM中的选定项绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我正在使用MVVM编写WPF应用程序。我有一个列表框,其中加载了多个用户控件。当我单击特定的列表框项时,将加载另一个控件(Control1)。我将关闭控件(Control1)。现在,当我再次单击相同的用户控件(在列表框中)时,它不会再次加载控件(Control1),这是因为我们再次选择了相同的项目时会触发selectedItem值。如何控制再次触发所选项目,以便我可以再次显示控件或有任何其他方法来解决此问题。



我的视图模型代码:



public TileControlViewModel SelectedItem

{

get {return _selectedItem; }

set

{

_selectedItem = value;

OnPropertyChanged(SelectedItem);

if(_selectedItem!= null)

{

AddToLiveTile(_selectedItem.Key);

}



}

}



我的尝试:



我尝试将所选项目更改为选定值,但仍无法正常工作。

解决方案

my XAML



< listbox name =lvDataBindingselectionmode =Singleitemssource ={Binding AllTiles}selecteditem ={Binding SelectedItem,Mode = TwoWay,UpdateSourceTrigger = PropertyChanged} issynchronizedwithcurrentitem =True>

< listbox.itemspanel> < itemspaneltemplate>

< wrappanel width ={Binding(FrameworkElement.ActualWidth),< br mode =hold =/> RelativeSource = {RelativeSource AncestorType = ScrollContentPresenter}}>





< listbox.itemtemplate> < datatemplate>

< custom:tilecontrol margin =10datacontext ={Binding}xmlns:custom =#unknown>



Hi All,

I am writing a WPF application using MVVM. I have a list box which has multiple user controls loaded into it. When i click on a particular listbox item another control(Control1) will be loaded. I will close the control(Control1). Now when i click on the same user control(in listbox) again it does not load the control(Control1) again this is because the selectedItem value is fired because we selected the same item again. How can i make control to fire selected item again so that i can display the control again or is there any other way to resolve this.

My View Model code:

public TileControlViewModel SelectedItem
{
get { return _selectedItem; }
set
{
_selectedItem = value;
OnPropertyChanged("SelectedItem");
if (_selectedItem != null)
{
AddToLiveTile(_selectedItem.Key);
}

}
}

What I have tried:

I tried changing selected item to selected value but still it is not working.

解决方案

my XAML

<listbox name="lvDataBinding" selectionmode="Single" itemssource="{Binding AllTiles}" selecteditem="{Binding SelectedItem,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" issynchronizedwithcurrentitem="True">
<listbox.itemspanel> <itemspaneltemplate>
<wrappanel width="{Binding (FrameworkElement.ActualWidth), <br mode=" hold=" /> RelativeSource={RelativeSource AncestorType=ScrollContentPresenter}}">


<listbox.itemtemplate> <datatemplate>
<custom:tilecontrol margin="10" datacontext="{Binding}" xmlns:custom="#unknown">


这篇关于wpff MVVM中的选定项绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆