如何绑定到2个不同的成员在WPF一类? [英] How to bind to 2 different members in a class in WPF?

查看:110
本文介绍了如何绑定到2个不同的成员在WPF一类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类,如:

 类EditorViewModel
{
    公众的ObservableCollection<效果> AllEffects;
    公共BOOL HasPermissions;
}

但问题是,当我试图绑定 AllEffects 的ListView ,那么我不能绑定什么 HasPermissions 因为绑定范围仅限于 AllEffects ,而不是 EditorViewModel

我试过,但它不工作:

 < ListView控件的ItemsSource ={结合EditorViewModel}>...< GridViewColumn WIDTH =自动标题=名称>
    < GridViewColumn.CellTemplate>
        <&DataTemplate的GT;
            < TextBlock的文本={结合AllEffects.Name}/>
        < / DataTemplate中>
    < /GridViewColumn.CellTemplate>
< / GridViewColumn>< GridViewColumn WIDTH =自动标题=类型>
    < GridViewColumn.CellTemplate>
        <&DataTemplate的GT;
            < TextBlock的文本={结合AllEffects.Type}/>
        < / DataTemplate中>
    < /GridViewColumn.CellTemplate>
< / GridViewColumn>

如果我设置了的ItemsSource EditorViewModel 和摆脱 AllEffects ,它的工作原理。但我不知道如何访问 HasPermissions 通过绑定:

 < GridViewColumn宽度=50标题=覆盖>
    < GridViewColumn.CellTemplate>
        <&DataTemplate的GT;
            <复选框保证金=0
                                    的Horizo​​ntalAlignment =中心
                                    IsEnabled ={结合HasPermission}/>
        < / DataTemplate中>
    < /GridViewColumn.CellTemplate>
< / GridViewColumn>


解决方案

一个经典的技巧是使用ViewModelLocator,请参阅:
<一href=\"http://stackoverflow.com/questions/3829418/mvvm-light-using-viewmodellocator-properties-hit-multiple-times\">MVVM灯 - ​​使用ViewModelLocator - 性能击中多次

此外,为了更快速和肮脏的解决方案,你可以使用下面的绑定:

  {绑定的RelativeSource = {的RelativeSource模式= FindAncestor,AncestorType =的ListView},路径= DataContext.HasPermissions}

请注意,这可能仅仅在WPF的工作,而不是在SL,SL以来不支持的RelativeSource这个语法。

I have a class like:

class EditorViewModel
{
    public ObservableCollection<Effect> AllEffects;
    public bool HasPermissions;
}

But the problem is, when I am trying to bind AllEffects to ListView, then I can't bind anything to HasPermissions because the binding scope is limited to AllEffects, not EditorViewModel.

I tried this but it doesn't work:

<ListView ItemsSource="{Binding EditorViewModel}">

...

<GridViewColumn Width="Auto" Header="Name">
    <GridViewColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding AllEffects.Name}"/>
        </DataTemplate>
    </GridViewColumn.CellTemplate>
</GridViewColumn>

<GridViewColumn Width="Auto" Header="Type">
    <GridViewColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding AllEffects.Type}"/>
        </DataTemplate>
    </GridViewColumn.CellTemplate>
</GridViewColumn>

If I set the ItemsSource to EditorViewModel and get rid of AllEffects, it works. But then I don't know how to access HasPermissions through binding:

<GridViewColumn Width="50" Header="Override">
    <GridViewColumn.CellTemplate>
        <DataTemplate>
            <CheckBox Margin="0"
                                    HorizontalAlignment="Center"
                                    IsEnabled="{Binding HasPermission}"/>
        </DataTemplate>
    </GridViewColumn.CellTemplate>
</GridViewColumn>

解决方案

A classic trick is to use ViewModelLocator, see: MVVM Light - using ViewModelLocator - properties hit multiple times

also, for a more quick-and-dirty solution, you can use the following Binding:

{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListView}, Path=DataContext.HasPermissions}

Note that this would only work on WPF and not in SL, since SL doesn't support this syntax of RelativeSource.

这篇关于如何绑定到2个不同的成员在WPF一类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆