开发者

Setting the objectinstance to the current data item

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>   

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 Ge开发者_如何学运维tDisplayItems 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;
    }


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜