开发者

WPF XAML No textbox display when binding

开发者 https://www.devze.com 2023-02-24 09:55 出处:网络
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 :

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 H开发者_运维技巧eaderTemplate="{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..


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>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号