tags:

views:

16

answers:

1

Hi,

I'm currently binding an xml file to a listbox in C# WPF. Within the itemtemplate I added controls. as follows:

<DataTemplate x:Key="SinglecueTemplate">
            <Grid Height="30" Width="425" Margin="3,3,0,3">
                <Button Content="{Binding XPath=nr}" Width="30" Style="{DynamicResource CUEStyle_Button_Inhoudknopje}" Template="{DynamicResource CUEStyle_singlecueknopnummer}" Height="Auto" HorizontalAlignment="Left" Background="#FFABCCED" Foreground="White" IsEnabled="False"/>
                <TextBlock x:Name="name" Margin="54,0,114.667,0" Width="Auto" VerticalAlignment="Center" FontSize="16" Foreground="Gray" Text="{Binding XPath=Name}"/>
                <Button x:Name="playbutton" Width="30" Style="{DynamicResource CUEStyle_Button_Groot}" Template="{DynamicResource CUEStyle_Rondknopje}" Height="Auto" HorizontalAlignment="Right" Margin="0,0.55,74.737,-0.55" Content="u" FontFamily="Wingdings 3" Foreground="#FF0178D3" Opacity="1" BorderBrush="#FF0178D3" Click="playcue"/>
                <Button Width="30" Style="{DynamicResource CUEStyle_Button_Groot}" Template="{DynamicResource CUEStyle_Rondknopje}" Height="Auto" HorizontalAlignment="Right" Margin="0,0.55,37.071,-0.55" Content="¢" FontFamily="Wingdings 2" Foreground="Gray" Opacity="0.4"/>
                <Button Width="15" Style="{DynamicResource CUEStyle_Button_Groot}" Template="{DynamicResource CUEStyle_kleinloopje}" Height="15" HorizontalAlignment="Left" Margin="15,0,0,-5.5" Content="Q" FontFamily="Wingdings 3" Foreground="White" FontWeight="Bold" FontSize="10.667" Opacity="1" VerticalAlignment="Bottom" BorderBrush="{x:Null}" Background="{x:Null}" IsEnabled="{Binding XPath=Loop}"/>
                <TextBlock Margin="0,0,114.667,0" Width="81.07" VerticalAlignment="Center" FontSize="16" Foreground="Gray" Text="03:02:11" HorizontalAlignment="Right"/>
            </Grid>
        </DataTemplate>

Now when a the playbutton is clicked I want to acces other properties from that listboxitem, for example the text from the text block. I looked up on how to do this and came up with the following:

            private void playcue(object sender, System.Windows.RoutedEventArgs e)
            {
                Button playcue = (Button)sender;
                Textbox titel = (Textbox)playcue.DataContext;
                MessageBox.Show(titel);
            }

But the code above gives me an error stating that the type Textbox is unknown. Do I have to do something with the datatemplate so that I can acces other items within the template? Or is it possible to access sibling nodes from the datasource?

UPDATE

Answer found. Now for future reference:

private void playcue(object sender, System.Windows.RoutedEventArgs e)
            {
                Button playcue = (Button)sender;
                XmlElement name = (XmlElement)playcue.DataContext; 
                MessageBox.Show(name.InnerText);

            }

Returns all sibling values from the item

If you want to access a specific sibling you can do so:

MessageBox.Show(name.SelectSingleNode("Name").InnerXml);
+1  A: 

First problem is that it's TextBox not Textbox (C# is case sensitive.)

Second, it's unlikely that your DataContext is a TextBox. It should be an item in your ItemsSource collection. So, if the user clicks the third button in the list, you should be looking at the 3rd item in your ItemsSource collection. Since it appears you are using XML as a data source, I would guess that the item should be an XElement or an XmlElement.

I suggest you change Textbox titel = (Textbox)playcue.DataContext; to the following:

XElement element = (XElement)playcue.DataContext;

or

XmlElement element = (XmlElement)playcue.DataContext;

You will also need to change your MessageBox statement as well.

DanM