![](/img/trans.png)
[英]Why C# WPF button binding command won't change view after using simple injector?
[英]C# Wpf simple listviewitem event binding won't work
我有一個非常簡單的列表視圖,其中 itemsource 綁定到一個集合。 然后我有一個委托命令(來自 Prism),當我點擊其中一個項目時我想調用它。 不幸的是,itemsource 中的綁定不起作用。 當我直接在列表視圖中設置attachedcommandbehavior 時,它會調用該方法。
也許有人可以幫忙。
<ListView ItemsSource="{Binding col}">
<ListView.ItemContainerStyle>
<Style>
<Setter Property="c:CommandBehavior.Event" Value="PreviewMouseDown"/>
<Setter Property="c:CommandBehavior.Command" Value="{Binding Command}"/>
</Style>
</ListView.ItemContainerStyle>
</ListView>
public ObservableCollection<string> col { get; set; }
public ICommand Command => _Command = _Command ?? new DelegateCommand<object>(e => cmd(e));
private ICommand _Command;
public void cmd (object e)
{
MessageBox.Show("Clicked");
}
public ViewModelCustom()
{
col = new ObservableCollection<string>();
col.Add("a");
col.Add("b");
col.Add("C");
}
謝謝
一種方法是在 ViewModel 中創建屬性並跟蹤 set 方法而不是命令中的更改
public T SelectedItem
{
get { return _selectedItem; }
set
{
_selectedItem = value;
// User selected another element here, do what you want
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.