RE: [dotnet-120] WPF Templated controls

From: user 4.
Sent on: Sunday, October 28, 2007 12:10 PM

The way of finding the checkbox manually is convoluted to say the least.  Here is code that should work for you:

 

        private void btnPurchase_Click(object sender, RoutedEventArgs e)

        {

            foreach (object listBoxItem in purchasingOptionList.Items)

            {

                ListViewItem container = purchasingOptionList.ItemContainerGenerator.ContainerFromItem(listBoxItem) as ListViewItem;

                GridViewRowPresenter rowPresenter = GetObjectOfTypeInVisualTree<GridViewRowPresenter>(container);

                ContentPresenter checkColumnPresenter = (ContentPresenter)VisualTreeHelper.GetChild(rowPresenter, 1);

                CheckBox tb = (CheckBox)(checkColumnPresenter.ContentTemplate.FindName("chkPurchase",checkColumnPresenter));

            }

        }

        private T GetObjectOfTypeInVisualTree<T>(DependencyObject dpob) where T : DependencyObject

        {

            int count = VisualTreeHelper.GetChildrenCount(dpob);

            for (int i = 0; i < count; i++)

            {

                DependencyObject child = VisualTreeHelper.GetChild(dpob, i);

                T childAsT = child as T;

                if (childAsT != null)

                {

                    return childAsT;

                }

                childAsT = GetObjectOfTypeInVisualTree<T>(child);

                if (childAsT != null)

                {

                    return childAsT;

                }

            }

            return null;

        }

 

I got the answer from combining a few blog posts.  The GetObjectOfTypeInVisualTree function came from Beatriz Costa’s blog.  Using the performance profiler from the .Net SDK is also helpful in seeing what is created at runtime for the visual tree.  The visual tree for a GridView is not intuitive.  By using a view you can bind two-way, you can avoid all of this mess.  I try to avoid taking dependencies on the layout of the window’s visual tree if I can help it.

 

From: [address removed] [mailto:[address removed]] On Behalf Of David Barnhill
Sent: Sunday, October 28,[masked]:23 AM
To: [address removed]
Subject: RE: [dotnet-120] WPF Templated controls

 

I think it would be easier to use a Model-ViewModel approach to this.  Instead of having a converter and manually finding what the user selected,  create a collection just for this list that has the name, price and selected state in it, then two way bind the selection.  That way you can read the selection directly from your bound list instead of digging though the UI elements. 

 

If that won’t work for you then I’ll see if I can find the manual way, but using WPF databinding to help you is a better way I think.

 

From: [address removed] [mailto:[address removed]] On Behalf Of Alec Bryte
Sent: Sunday, October 28,[masked]:08 AM
To: [address removed]
Subject: [dotnet-120] WPF Templated controls

 

Hi Guys,

 

Do you know how to get a reference to a control in a template item?

I have a  ListView bound to a collection of custom objects. One of the columns contains a CheckBox control which doesn't have a corresponding property in the data source. It simply indicates whether or not user wants to select a particular item. Upon a button click, I want to go through all rows in the grid and collect the IsChecked state of that CheckBox for each item.

 

Since the checkbox is inside a template, its name is auto-generated for each row.

I've seen several examples that use FindName() method but I couldn't get them to work. Here's my code.

Thanks,

-Alec.

 

         <ListView Name="purchasingOptionList" Margin="0,[masked],0,80.745">
            <ListView.View>
                <GridView>
                    <GridViewColumn>
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <StackPanel>
                                    <TextBlock Text="{Binding Path=Name}" FontWeight="Bold"></TextBlock>
                                    <TextBlock Margin="5,0,0,0" Text="{Binding Path=Description}" FontStyle="Italic"></TextBlock>
                                </StackPanel>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Header="Price" DisplayMemberBinding="{Binding Path=Price}" />
                    <GridViewColumn>
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <CheckBox Tag="{Binding}" Name="chkPurchase"
                                          IsEnabled="{Binding Mode=OneWay, Converter={StaticResource IsPurchaseOptionEnabledConverter}}"
                                          IsChecked="{Binding Mode=OneWay, Converter={StaticResource IsPurchaseOptionCheckedConverter}}" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>
<Button Height="23" HorizontalAlignment="Left" Margin="25.377,0,0,13.842" Name="btnPurchase" VerticalAlignment="Bottom" Width="[masked]" Click="btnPurchase_Click">Make Purchase</Button>


        private void btnPurchase_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
            foreach (object listBoxItem in purchasingOptionList.Items)
            {
                ListViewItem container = purchasingOptionList.ItemContainerGenerator.ContainerFromItem(listBoxItem) as ListViewItem;
                object value = container.FindName("chkPurchase");
            }
        }





--
Please Note: If you hit "REPLY", your message will be sent to everyone on this mailing list ([address removed])
This message was sent by Alec Bryte ([address removed]) from New York .NET Meetup Group.
To learn more about Alec Bryte, visit his/her member profile
To unsubscribe or to update your mailing list settings, click here

Meetup.com Customer Service: [address removed]
632 Broadway New York NY 10012 USA





--
Please Note: If you hit "REPLY", your message will be sent to everyone on this mailing list ([address removed])
This message was sent by David Barnhill ([address removed]) from New York .NET Meetup Group.
To learn more about David Barnhill, visit his/her member profile
To unsubscribe or to update your mailing list settings, click here

Meetup.com Customer Service: [address removed]
632 Broadway New York NY 10012 USA

People in this
Meetup are also in:

Sign up

Meetup members, Log in

By clicking "Sign up" or "Sign up using Facebook", you confirm that you accept our Terms of Service & Privacy Policy