locked
Binding Items from Templates on a ListView in code RRS feed

  • Question

  • I have a UserControl that contains one ListView and 3 buttons. Each button has an ObservableCollection attached as its tag. The collections each contain different types of objects.  When you click the button, the button.tag gets assigned to the ListView.ItemSource. 

    Now, the ListViewItems presentation is controlled by a DataTemplate. It has 4 TextBlocks and I originally had set up the template for just one of the object types.

    its XAML was this: (Edit: I assign the PrinterList.ItemsSource in code.)

            <DataTemplate x:Key="PrinterListItemTemplate">
                <Grid Height="48" MinWidth="752" Margin="0,0,0,-10">
                    <Grid.Background>
                        <ImageBrush Stretch="Fill" ImageSource="Assets/gray_texture.png"/>
                    </Grid.Background>
                    <Rectangle Fill="{StaticResource MainColorBrush}" Height="2" VerticalAlignment="Bottom"/>
                    <TextBlock HorizontalAlignment="Left" Margin="10,2,0,0" TextWrapping="Wrap" Text="{Binding PrinterName}" VerticalAlignment="Top" Width="160" FontSize="14" Height="39"/>
                    <TextBlock HorizontalAlignment="Left" Margin="175,2,0,0" TextWrapping="Wrap" Text="{Binding PrinterIp}" VerticalAlignment="Top" Width="140" Height="39"/>
                    <TextBlock HorizontalAlignment="Left" Margin="445,2,0,0" TextWrapping="Wrap" Text="{Binding PrinterType}" VerticalAlignment="Top" Width="140" Height="39"/>
                    <TextBlock HorizontalAlignment="Left" Margin="320,2,0,0" TextWrapping="Wrap" Text="{Binding PrinterLocation}" VerticalAlignment="Top" Width="120" Height="39"/>
                    <Button Content="X" Tag="{Binding PrinterName}" HorizontalAlignment="Right" Margin="0,-1,7,0" VerticalAlignment="Top" BorderBrush="{StaticResource RedButtonColorBrushDarker}" Foreground="{StaticResource RedColorButtonBrush}" Height="45" Padding="10,0" Click="Printer_Click"/>
                </Grid>
            </DataTemplate>

    I was attempting to do something like this (without success):

    the C#:

            private void btnItem_Click(object sender, RoutedEventArgs e)
            {
                EntryList.ItemsSource = _app.ItemCollection;
                value1Binding = "{Binding ItemId}";
                value2Binding = "{Binding Upc}";
                value3Binding = "{Binding ItemDescription}";
                value4Binding = "{Binding RetailPrice}";
            }
    
            private void btnUsers_Click(object sender, RoutedEventArgs e)
            {
                EntryList.ItemsSource = _app.UserCollection;
                value1Binding = "{Binding Id}";
                value2Binding = "{Binding FirstName}";
                value3Binding = "{Binding LastName}";
                value4Binding = "{Binding Code}";
            }

    the XAML:

                    <TextBlock HorizontalAlignment="Left" Margin="10,2,0,0" TextWrapping="Wrap" Text="{Binding value1Binding}" VerticalAlignment="Top" Width="160" FontSize="14" Height="39"/>
                    <TextBlock HorizontalAlignment="Left" Margin="175,2,0,0" TextWrapping="Wrap" Text="{Binding value2Binding}" VerticalAlignment="Top" Width="140" Height="39"/>
                    <TextBlock HorizontalAlignment="Left" Margin="445,2,0,0" TextWrapping="Wrap" Text="{Binding value3Binding}" VerticalAlignment="Top" Width="140" Height="39"/>
                    <TextBlock HorizontalAlignment="Left" Margin="320,2,0,0" TextWrapping="Wrap" Text="{Binding value4Binding}" VerticalAlignment="Top" Width="120" Height="39"/>
                    <Button Content="X" Tag="{Binding value1Binding}" HorizontalAlignment="Right" Margin="0,-1,7,0" VerticalAlignment="Top" BorderBrush="{StaticResource RedButtonColorBrushDarker}" Foreground="{StaticResource RedColorButtonBrush}" Height="45" Padding="10,0" Click="Printer_Click"/>

    Is it possible to change the specific Binding Value in code on a button click?

    Where did I go wrong?


    • Edited by 3itg Monday, December 9, 2013 11:49 PM To clarify initial ItemsSource
    Monday, December 9, 2013 10:07 PM

Answers

  • To create the binding from code you'll create Binding object and set its properties as C# properties rather than setting a Xaml string.

    To access the template information depends on where you're using the template. You wouldn't generally modify the template itself, but would find the objects created by the template and modify them. If you want to do this as the objects are created then check out the PrepareContainerForItemOverride method

    --Rob

    Thursday, December 12, 2013 2:36 AM
    Moderator

All replies

  • The syntax you are using is valid only in Xaml, not in code. See Creating bindings in code for information on binding from code.

    --Rob

    Tuesday, December 10, 2013 1:21 AM
    Moderator
  • Thank you for your response Rob,

    I understand that the syntax is incorrect. 

    Even after looking at the link you provided, I am unable to discern what the proper syntax should be for what I am trying to accomplish.

    In the end, I want something in XAML that says {Binding Upc} to say {Binding CategoryName} instead.

    each change takes place in a button click, so my thinking was that I could just use that interaction to change the Binding. But as you said, the syntax I am trying to use is not valid.

    I moved on from what I was attempting since I posted that, and I made 3 different Templates with the Bindings set appropriately.

    I am getting stuck in a similar place trying to select from the data templates on the fly, I am unable to figure out how these templates, that are defined in XAML can be accessed via c#...

    Clarification: One of the reasons I am failing to understand the examples I am to find, like the one you listed, is because I am using a Template, and I am unable to figure out how to access the template information in code as well.


    • Edited by 3itg Tuesday, December 10, 2013 1:51 AM Clarification
    Tuesday, December 10, 2013 1:43 AM
  • To create the binding from code you'll create Binding object and set its properties as C# properties rather than setting a Xaml string.

    To access the template information depends on where you're using the template. You wouldn't generally modify the template itself, but would find the objects created by the template and modify them. If you want to do this as the objects are created then check out the PrepareContainerForItemOverride method

    --Rob

    Thursday, December 12, 2013 2:36 AM
    Moderator
  • Thanks for your efforts in answering my question.

    I was never able to figure out how to use the bindings dynamically.

    I ended up using 3 templates, each setup with its respective bindings, then toggle their visibility on button click in code.

    It ended up looking identical to what I wanted, just not coded the way the I thought it should be...

    Wednesday, December 18, 2013 8:22 PM