![](/img/trans.png)
[英]Static property of inner static class of non-static class shared between instances of the non-static class?
[英]How to data bind to a static property on a non-static class?
在我的ViewModel
類中,我有一個靜態屬性AllSupport
但我不知道如何正確綁定它。 ListView 已綁定到具有AllSupport
靜態屬性的 ObservableCollection AllEffects
。
我用過這個:
<GridViewColumn
Width="Auto"
Header="GPU">
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox
Margin="0"
HorizontalAlignment="Center"
IsChecked="{Binding AllSupport[HardwareType].SupportList.IsSupported, Mode=TwoWay}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
AllEffects
是ObservableCollection
的EffectViewModel
其中它具有稱為靜態屬性AllSupport
其是類型: Dictionary<HardwareType, List<EffectSupport>>
其中:
HardwareType
是一個枚舉,而EffectSupport
是一個實例類,它有一個名為IsSupported
的布爾屬性。
我也試過這個,但后來它抱怨它在ViewModel
類上找不到IsSupported
:
IsChecked="{Binding AllSupport[HardwareType].SupportList, Path=IsSupported
任何想法,如何指定此綁定?
無論類是否為靜態,您都可以使用x:Static
來訪問靜態成員。
未經測試:
IsChecked="{Binding [HardwareType], Source={x:Static prefix:EffectViewModel.AllSupport}}"
並且您需要一個prefix
來訪問您的視圖模型的命名空間。
這是我的場景:
問題陳述:
我的解決方案:
后面的代碼(Window.cs):
public ObservableCollection<T> FooList {get {return FooLogger.ExceptionList;}}
//where FooLogger is a non-static class
//and ExceptionList is a static ObservableCollection<T>
數據上下文(Window.cs):
this.DataContext=this;
XAML(窗口.xaml)
<ListView ItemsSource="{Binding FooList}">
<ListView.View>
<GridView>
<GridViewColumn Width="Auto" Header="Name" DisplayMemberBinding="{Binding Name}" />
干杯,v0k
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.