简体   繁体   English

绑定到后面的代码中的方法

[英]Bind to a method in code behind

I have a custom control template for a ListView that puts an extra line in for each record, thats defined something like this in Window.Resources... 我有一个ListView的自定义控件模板,为每条记录添加一条额外的行,在Window.Resources中定义了类似的东西......

<ControlTemplate TargetType="ListBoxItem">
    <Border>
        <StackPanel>
            <GridViewRowPresenter>
            <TextBlock Name="myTextBlock" />
        </StackPanel>
     </Border>
     <ControlTemplate.Triggers>
         //Triggers here
     </ControlTemplate.Triggers>
</ControlTemplate>

My problem is that I want to bind the text in the TextBlock to a different ItemsSource than the one that will be bound to the actual ListBox when its instantiated. 我的问题是我想将TextBlock的文本绑定到一个不同的 ItemsSource不是在实例化时绑定到实际ListBox那个。 Binding programatically is impossible. 以编程方式绑定是不可能的。 I've tried substituting the TextBlock for another ListView and binding to a method, but I couldn't work out how to use ObjectDataProvider and bind to a method in my code behind (which contains a method that would return a list of things I want to bind too), but ran into problems with this as well. 我已经尝试将TextBlock替换为另一个ListView并绑定到一个方法,但我无法弄清楚如何使用ObjectDataProvider并绑定到我后面的代码中的方法(其中包含一个返回我想要的列表的方法)绑定也),但也遇到了问题。

A quick step by step in case I'm not being clear: 如果我不清楚,请快速一步一步:

  • I have a ListView template that adds an extra row for each record 我有一个ListView模板,为每条记录添加一个额外的行

  • This ListView will be bound to (say) a collection of Foo objects. 这个ListView将被绑定到(比方说)Foo对象的集合。

  • Problem is I then want to bind the extra row to a completely different itemsource than the main ListView . 问题是我想要将额外的行绑定到与主ListView完全不同的itemsource。 It doesnt seem like I can do this from within my template :/ 我似乎无法在我的模板中执行此操作:/

So - is there a way to straight up bind to the results of a method defined in my code behind, which I could reference in the template? 那么 - 有没有办法直接绑定到我后面的代码中定义的方法的结果,我可以在模板中引用它?

Ok, that is the idea: 好的,这就是想法:

1) create list box which is bound to Foo with such item template: 1)创建与这样的项目模板绑定到Foo列表框:

-------------
| DATA HERE |
|           |
-------------

2) create list box which is bound to completely different itemsource with such item template: 2)创建列表框,该列表框与这样的项目模板绑定到completely different itemsource项目源:

-------------
|           |
| DATA HERE |
-------------

3) draw the first list box behind the second one. 3)绘制第二个列表框后面的第一个列表框。

If there are the same count of items (this is your case as I understand), you will achieve the visual effect you want. 如果项目数相同(这是我理解的情况),您将获得所需的视觉效果。 Hope it helps. 希望能帮助到你。

EDIT 编辑
This method does not correspond your present template, but it is a variant of solution. 此方法与您当前的模板不对应,但它是解决方案的变体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM