[英]Using ListBoxItemExtensions.IsSelected from WinRt Xaml Toolkit doesn't work TwoWay Binding
在帶有WinRt Xaml Toolkit的 Windows RT App(c#)中,我使用以下命令:
<ListBox ItemsSource="{Binding Path=FilterBaseFields}" SelectionMode="Multiple">
<ListBox.ItemTemplate >
<DataTemplate >
<TextBox Text="{Binding Path=Key, Mode=TwoWay}"
extensions:ListBoxItemExtensions.IsSelected="{Binding Path=IsSelected, Mode=TwoWay}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
對於文本TwoWay綁定,效果很好,但對於擴展:ListBoxItemExtensions.IsSelected-不起作用(僅從源到視圖有效)。
有什么想法嗎?
我問它已經在WinRT XAML工具包討論中 ,該工具包的作者Filip Skakun回答:
那就對了。 這似乎是對此屬性的限制。 我們將需要創建另一種來支持雙向綁定。 類似於使用此處所述的模式的“ IsSelectedBinding”屬性: http ://blog.onedevjob.com/2011/10/26/workaround-for-binding-not-supported-on-stylesettervalue-in-silverlight/也許在什么時候我有機會將其添加到工具箱中。
看起來像:
public class MyListBox : ListBox
{
protected override void PrepareContainerForItemOverride(
DependencyObject element, object item)
{
base.PrepareContainerForItemOverride(element, item);
if (item is Item)
{
var binding = new Binding
{
Source = item,
Path = new PropertyPath("IsSelected"),
Mode = BindingMode.TwoWay
};
((ListBoxItem)element).SetBinding(ListBoxItem.IsSelectedProperty, binding);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.