WPF Listview 样式问题 ?
<Grid> <Grid.Resources> <Style x:Key="styledItems" TargetType="{x:Type ListBox}"> <Setter Property="ItemTemplate"> <Setter.Value> <DataTemplate> <Grid> <Image Width="48" Source="{Binding ImagePath}" /> <TextBlock Text="{Binding EnglishName}"/> </Grid> </DataTemplate> </Setter.Value> </Setter> </Style> </Grid.Resources> <ListBox ItemsSource="{Binding}" Style="{StaticResource styledItems}" Margin="92,58,80,74" Name="listBox1" /> </Grid>
<Grid> <Image Width="48" Source="{Binding ImagePath}" /> <TextBlock Text="{Binding EnglishName}"/> </Grid>
[解决办法]
Grid布局里面,都是默认从面板的(0,0)位置摆放,解决方法很多:
1、设置他们的摆放位置:<Image Margin="0,0,5,0"..../><TextBlock Margin="50,0,0,0.../>
2、换布局,楼上用stackpanel就可以实现两个控件按顺序摆放,或者你在grid里面增加两行
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
然后在Image和TextBlock加入Grid.Row="",就可以了。