WPF XAML No textbox display when binding

1.4k views Asked by At

I'm having hard times with a simple textbox not displaying what I want. Basically I have a grouped listbox from this data file xml :

<Hosts>
  <Host foo="aaa">
    <usable>1</usable>
  </Host>
  <Host foo="bbb">
    <usable>1</usable>
  </Host>
</Hosts> 

I have the following code then :

<CollectionViewSource x:Key="cvs"
                      Source="{Binding Source={StaticResource HostsData}}">
    <CollectionViewSource.GroupDescriptions>
        <PropertyGroupDescription PropertyName="@foo" />
    </CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
<DataTemplate x:Key="categoryTemplate">
    <TextBlock Text="test"
               FontWeight="Bold"
               Background="Gold"
               Margin="0,5,0,0" />
</DataTemplate> 

...

<ListBox Name="myList"
         Grid.Row="0"
         Grid.Column="1"
         TextBlock.FontSize="9"
         Margin="2"
         ItemsSource="{Binding Source={StaticResource cvs}}"
         ItemTemplate="{StaticResource MachinesTemplate}">
    <ListBox.GroupStyle>
        <GroupStyle HeaderTemplate="{StaticResource categoryTemplate}" />
    </ListBox.GroupStyle>
</ListBox>

So I have my grouped listbox, but the testbox content is empty. It's "gold" and if I setup Text="test" it's bolded as it's supposed to, but I can't get it to display the "foo" content (aaa, bbb).

I've tried all sort of binding without success so far..

1

There are 1 answers

0
John Bowen On BEST ANSWER

To get the text of the matched property you need to bind to the Name property inside the GroupStyle HeaderTemplate:

<TextBlock Text="{Binding Path=Name}" ... />

Getting to that point assumes that all of your XPaths are working properly, which is a whole other set of issues. Here's a full working simplified example with the relevant parts from your code:

<Grid>
    <Grid.Resources>
        <XmlDataProvider x:Key="HostsData"
                         XPath="//Host">
            <x:XData>
                <Hosts xmlns="">
                    <Host foo="aaa">
                        <usable>1</usable>
                    </Host>
                    <Host foo="bbb">
                        <usable>1</usable>
                    </Host>
                </Hosts>
            </x:XData>
        </XmlDataProvider>
        <CollectionViewSource x:Key="cvs"
                              Source="{Binding Source={StaticResource HostsData}}">
            <CollectionViewSource.GroupDescriptions>
                <PropertyGroupDescription PropertyName="@foo" />
            </CollectionViewSource.GroupDescriptions>
        </CollectionViewSource>
        <DataTemplate x:Key="categoryTemplate">
            <TextBlock Text="{Binding Path=Name}"
                       FontWeight="Bold"
                       Background="Gold"
                       Margin="0,5,0,0" />
        </DataTemplate>
    </Grid.Resources>
    <ListBox Name="myList"
             ItemsSource="{Binding Source={StaticResource cvs}}">
        <ListBox.GroupStyle>
            <GroupStyle HeaderTemplate="{StaticResource categoryTemplate}" />
        </ListBox.GroupStyle>
    </ListBox>
</Grid>