簡體   English   中英

如何將數據綁定到非靜態類上的靜態屬性?

[英]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>

AllEffectsObservableCollectionEffectViewModel其中它具有稱為靜態屬性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來訪問您的視圖模型的命名空間。

這是我的場景:

  • 有一個帶有靜態屬性的非靜態類,它是一個 ObservableCollection
  • 該類用作記錄器,並從程序中收集特定異常

問題陳述:

  • 如何將非靜態類的靜態成員綁定到 XAML 中的列表視圖

我的解決方案:

后面的代碼(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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM