addressalign-toparrow-leftarrow-rightbackbellblockcalendarcameraccwcheckchevron-downchevron-leftchevron-rightchevron-small-downchevron-small-leftchevron-small-rightchevron-small-upchevron-upcircle-with-checkcircle-with-crosscircle-with-pluscrossdots-three-verticaleditemptyheartexporteye-with-lineeyefacebookfolderfullheartglobegmailgooglegroupshelp-with-circleimageimagesinstagramlinklocation-pinm-swarmSearchmailmessagesminusmoremuplabelShape 3 + Rectangle 1ShapeoutlookpersonJoin Group on CardStartprice-ribbonShapeShapeShapeShapeImported LayersImported LayersImported Layersshieldstartickettrashtriangle-downtriangle-uptwitteruserwarningyahoo

RE: [dotnet-120] WPF Templated controls

From: user 4.
Sent on: Sunday, October 28, 2007 10:22 AM

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.




         <ListView Name="purchasingOptionList" Margin="0,[masked],0,80.745">
                                    <TextBlock Text="{Binding Path=Name}" FontWeight="Bold"></TextBlock>
                                    <TextBlock Margin="5,0,0,0" Text="{Binding Path=Description}" FontStyle="Italic"></TextBlock>
                    <GridViewColumn Header="Price" DisplayMemberBinding="{Binding Path=Price}" />
                                <CheckBox Tag="{Binding}" Name="chkPurchase"
                                          IsEnabled="{Binding Mode=OneWay, Converter={StaticResource IsPurchaseOptionEnabledConverter}}"
                                          IsChecked="{Binding Mode=OneWay, Converter={StaticResource IsPurchaseOptionCheckedConverter}}" />
<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 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