Binding Dictionary> to ListBox in WPF

后端 未结 2 1442
慢半拍i
慢半拍i 2021-01-15 18:49

I have a Dictionary> where the key is the hostname of a machine that writes to a server, and the value is an list of Wr

相关标签:
2条回答
  • 2021-01-15 19:17

    For your ItemsControl you need to set an ItemTemplate:

            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock>Server: </TextBlock>
                        <TextBlock Text="{Binding Server}"/>
                    </StackPanel>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
    

    That will get you started.

    0 讨论(0)
  • 2021-01-15 19:43

    Set DataContext property for inner control to show WritingMachine properties. Like so :

    <StackPanel DataContext="{Binding Value}" Grid.Row="1">
    

    This will change the DataContext to List<WritingMachine> for inner display control, where you can do normal binding. See full XAML code below :

    <ListBox x:Name="LbxMachineDictionary" HorizontalAlignment="Left" VerticalAlignment="Top" Grid.Row="1">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Vertical" Margin="5">
                            <Grid Width="461" Height="199">
                                <Grid.RowDefinitions>
                                    <RowDefinition/>
                                    <RowDefinition Height="4*"/>
                                </Grid.RowDefinitions>
                                <TextBlock HorizontalAlignment="Left" Margin="10,10,0,0" TextWrapping="Wrap" Text="{Binding Key}" VerticalAlignment="Top"/>
                                <StackPanel DataContext="{Binding Value}" Grid.Row="1">
                                    <TextBlock HorizontalAlignment="Left"  Grid.Row="1" TextWrapping="Wrap" Text="{Binding HostName}" VerticalAlignment="Top"/>
                                    <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding Program}" VerticalAlignment="Top"/>
                                    <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding Login}" VerticalAlignment="Top"/>
                                    <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding Server}" VerticalAlignment="Top"/>
                                </StackPanel>
                            </Grid>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
    
    0 讨论(0)
提交回复
热议问题