将objectinstance设置为当前数据项 [英] Setting the objectinstance to the current data item

查看:67
本文介绍了将objectinstance设置为当前数据项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对WPF还是很陌生,一直在努力寻找答案的这几天没有太多运气,看来应该有办法了.我已经建立了一个DataTemplate,其DataType是我的自定义类.在DataTemplate定义中,我已经使用设置了资源集合.之所以这样做,是因为我想创建一个ObjectDataProvider,该控件可用于DataTemplate中的控件-我希望此ObjectDataProvider的ObjectInstance当前绑定到数据项(自定义类的列表中的当前实例)中-因为那么我希望能够在当前数据实例上运行一种方法-当用户更改属于DataTemplate的文本框中的文本时.很难解释,但这应该使它更清楚,这是我的xaml:

I am fairly new to WPF, have been working on finding an answer to this for a couple days without much luck, it seems like there should be a way. I have set up a DataTemplate whose DataType is a custom class of mine. Within the DataTemplate definition, I have set up a resources collection using . I did this because I want to create an ObjectDataProvider that will be available to the controls in the DataTemplate - I want the ObjectInstance of this ObjectDataProvider, to be currently bound data item (teh current instance within a list, of my custom class) - because then I want to be able to run a method on the current data instance - when the user changes the text in a textbox that is part of the DataTemplate. Hard to explain but this should make it clearer, here is my xaml:

    <DataTemplate x:Key="TierDisplay" DataType="{x:Type tiers:PopulatedTier}">
        <DataTemplate.Resources>
            <ObjectDataProvider x:Key="FilteredItems"  MethodName="GetDisplayItems">
                <ObjectDataProvider.MethodParameters>
                    <sys:Int32>0</sys:Int32>
                </ObjectDataProvider.MethodParameters>
            </ObjectDataProvider> 
        </DataTemplate.Resources>
        <StackPanel Orientation="Vertical">
            <TextBox Name="txtMaxSupplyDays" LostFocus="txtMaxSupplyDays_LostFocus"></TextBox>
            <DataGrid ItemsSource="{Binding Source={StaticResource FilteredItems}}" />
        </StackPanel>
    </DataTemplate>   

每个DataTemplate实例都绑定到PopulatedTier类的一个实例.当用户离开文本框txtMaxSupplyDays时,我在代码后面的代码中采用了他们输入的值,并将其放入我的ObjectDataProvider的第一个MethodParameter中(其键为FilteredItems).使用下面的C#代码可以正常工作,该代码找到FilteredItems并将所需的值插入到MethodParameter中.但是我不知道如何将FilteredItems绑定到PopulatedTier的当前实例中,以便其GetDisplayItems能够运行.(如果这行得通,那么大概可以使用GetDisplayItems的输出作为其ItemsSource刷新DataGrid.)实际上,在下面的C#中,它找到/识别出文本框(发送者)的DataContext属性是PopulatedTier的一个实例.但是,如何在ObjectDataProvider定义的XAML中引用它呢?谢谢,让我知道是否可以进一步澄清.欢迎提出其他建议.我想尽可能地将XAML和代码隐藏在后面.

Each instance of the DataTemplate is bound to an instance of the PopulatedTier class. When the user leaves the textbox, txtMaxSupplyDays, I have code in the code-behind to take the value they have entered, and put it into the first MethodParameter of my ObjectDataProvider (whose key is FilteredItems). This works fine using the C# code-behind below, the code finds FilteredItems and plugs the desired value into the MethodParameter. But I can't figure how to tie FilteredItems into the current instance of PopulatedTier so that its GetDisplayItems will run. (If this worked, then presumably the DataGrid would refresh, using the output of GetDisplayItems as its ItemsSource.) In fact, in the C# below, it finds/recognizes the DataContext property of the textbox (sender) as being an instance of PopulatedTier. But how can I refer to this in the XAML within the ObjectDataProvider definition? THANK YOU and let me know if I can clarify further. Of cousre alternate suggestions are welcome; I'd like to keep as much in the XAML and out of the code-behind as I can.

    private void txtMaxSupplyDays_LostFocus(object sender, RoutedEventArgs e)
    {
        var textBox = sender as TextBox;
        if (textBox == null) return;
        int value;
        bool valueOK = Int32.TryParse(textBox.Text, out value);

        if (valueOK)
        ((ObjectDataProvider)textBox.FindResource("FilteredItems")).MethodParameters[0] = value;
    }

推荐答案

您对代码隐藏有正确的想法-它必须尽可能小.它是MVVM模式的口号之一,那就是您需要的-学习MVVM.互联网有很多资源,因此找到它不是问题.

You have right thoughts about your code-behind - it have to be as small as possible. Its one of the slogan of MVVM pattern, that is what you need - learn MVVM. Internet have a lot of resources, so it wouldn't be a problem to find it.

这篇关于将objectinstance设置为当前数据项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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