正如您所看到的,我的应用中有两个DataGrid个。由于未知原因,应用程序启动时,一个或两个网格的大小不正确。 (图像以编程方式加载。)
有谁知道为什么,更重要的是,有办法解决这个问题?
奇怪的是,当滚动鼠标滚动按钮时,网格会自行调整大小,但滚动按钮不会滚动网格(只有向上/向下翻页和光标键,当然,除了拖动滚动条)。此外,即使删除ScrollViewer
,问题仍然存在。
<UserControl x:Class="SW.ImagesControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:SW="clr-namespace:SW">
<UserControl.Resources>
<SW:NullImageConverter x:Key="NullImageConverter" />
</UserControl.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<ScrollViewer HorizontalScrollBarVisibility="Visible" Grid.Row="1" Grid.ColumnSpan="2">
<DataGrid AutoGenerateColumns="False" IsReadOnly="True" Padding="0" Margin="0"
ItemsSource="{Binding}" SelectionUnit="Cell"
VirtualizingStackPanel.VirtualizationMode="Recycling" >
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}" />
<DataGridTemplateColumn Header="Image">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Command="{Binding ChangeImageCommand}">
<Button.Template>
<ControlTemplate>
<Grid>
<TextBlock Name="tb" HorizontalAlignment="Center" VerticalAlignment="Center" Visibility="Collapsed">
<Hyperlink Command="{Binding ChangeImageCommand}">Browse...</Hyperlink>
</TextBlock>
<Image Name="img" Source="{Binding Image, Converter={StaticResource NullImageConverter}}" Margin="0"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger SourceName="img" Property="Source" Value="{x:Null}">
<Setter TargetName="tb" Property="Visibility" Value="Visible"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Button.Template>
</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Preview">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image Source="{Binding PreviewImage, Converter={StaticResource NullImageConverter}}" Margin="0" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</ScrollViewer>
</Grid>
</UserControl
答案 0 :(得分:1)
DataGrid
有一个内部ScrollViewer
,因此不需要外部VirtualizingStackPanel.VirtualizationMode
。但这可能不是很明显,因为Recycling
设置为<UserControl x:Class="SW.ImagesControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:SW="clr-namespace:SW">
<UserControl.Resources>
<SW:NullImageConverter x:Key="NullImageConverter" />
</UserControl.Resources>
<DataGrid AutoGenerateColumns="False" IsReadOnly="True" Padding="0" Margin="0"
ItemsSource="{Binding}" SelectionUnit="Cell" >
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}" />
<DataGridTemplateColumn Header="Image">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Command="{Binding ChangeImageCommand}">
<Button.Template>
<ControlTemplate>
<Grid>
<TextBlock Name="tb" HorizontalAlignment="Center" VerticalAlignment="Center" Visibility="Collapsed">
<Hyperlink Command="{Binding ChangeImageCommand}">Browse...</Hyperlink>
</TextBlock>
<Image Name="img" Source="{Binding Image, Converter={StaticResource NullImageConverter}}" Margin="0"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger SourceName="img" Property="Source" Value="{x:Null}">
<Setter TargetName="tb" Property="Visibility" Value="Visible"/>
<!--<Setter TargetName="img" Property="Visibility" Value="Collapsed"/>-->
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Button.Template>
</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Preview">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image Source="{Binding PreviewImage, Converter={StaticResource NullImageConverter}}" Margin="0" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</UserControl
。删除该属性和滚动查看器可以解决大小调整问题以及缺少滚动问题。
{{1}}