开发者

How to bind a static property in a different class to a GridViewColumn of WPF ListView?

Firstly I am using this for the ListView control itself:

ItemsSource="{Binding AllEffects}"

where 3 GridViewColumns already binded to to AllEffects.

But I have 2 more GridViewColumns that I want to bind to a separate static property found in:

public static class AllSupport
{
    public static EffectSupportLookup<HardwareType, List<EffectSupport>>
}

public class EffectSupport
{
    public bool IsSupported开发者_如何学运维 {get;set;}
}

I have tried this:

<GridViewColumn
    Width="Auto"
    Header="GPU">
    <GridViewColumn.CellTemplate>
        <DataTemplate>
            <CheckBox
                Margin="0"
                HorizontalAlignment="Center"
                IsChecked="{Binding AllSupport, Mode=TwoWay}"/>
        </DataTemplate>
    </GridViewColumn.CellTemplate>
</GridViewColumn>

But at runtime, it complains that the there is no property called AllSupport on AllEffects. I don't want to store it inside AllEffects because this is a separate class already compatible with the UI, so I just want to bind it to:

AllSupport.EffectSupport[GPU].IsSupported

Any ideas?


Use x:Static Markup Extension.

Something like (never tested):

<Window xmlns:local="AssemblyName">
  <ItemsControl 
    ItemsSource="{Binding Source={x:Static local:AllSupport.EffectSupport}}" />
</Window>

Or thru to a CollectionViewSource.

Update:

  1. You have to specify internal path
  2. Are you sure the local xmlns points to the right clr namespace (get assistance from the VS Xaml Intellisense)
  3. Does your app compile before setting it in xaml?
  4. Be more specific with your generic class implmenetation, what are you trying to achieve? is it a dictionary? a generic class? please reedit your code to give us the right look of your scenario.


I have 2 more GridViewColumns that I want to bind to a separate static property

I don't think you can do that, 1 ItemsSource means 1 SourceCollection.

But you can easily use LINQ to create a ad-hoc collection that includes those 2 columns

I don't want to store it inside AllEffects because this is a separate class already compatible with the UI,

If this means it's a ViewModel class, and if you want those columns in the View, then that is a very strong reason they should be stored in that class. Or in a separate, derived, ViewModel.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜