我的 WPF 应用程序将时间线显示为 UserControl,因此使用滚动查看器。
UserControl 的宽度取决于某些值,因此可以是 0 像素宽到 N 宽。
我遇到的问题是让 WPF 知道它的宽度。
<UserControl x:Class="TimeLineCanvas.UserControls.TimeLine"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:helper="clr-namespace:TimeLineCanvas.Helpers"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
DataContext="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorLevel=1,AncestorType=Window}}">
<Grid>
<Grid.Resources>
<ResourceDictionary Source="../ResourceDictionary.xaml" />
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition Height="300" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="450"></ColumnDefinition>
<ColumnDefinition Width="200"></ColumnDefinition>
</Grid.ColumnDefinitions>
<ScrollViewer HorizontalScrollBarVisibility="Auto" Grid.Row="0" Grid.Column="0" Margin="10">
<Grid>
<!--<Grid HorizontalAlignment="Left" Width="500">-->
<Grid.RowDefinitions>
<RowDefinition Height="40" />
<RowDefinition Height="110" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Canvas x:Name="MyCanvasMarkers" Grid.Row="0" VerticalAlignment="Top" Grid.Column="0" /> <!-- this will always be the widest of the canvas's-->
<Canvas x:Name="MyCanvasShowEvents" Grid.Row="1" Grid.Column="0" />
<Canvas x:Name="MyCanvasFailEvents" Grid.Row="1" Grid.Column="0" />
</Grid>
</ScrollViewer>
<!-- I doubt anything below this is useful for this post but I keep it in case I'm wrong-->
<Grid x:Name="ConfigurationGrid" Grid.Column="1" Grid.Row="0" >
<StackPanel>
<Expander IsExpanded="True">
<Expander.Header>
<TextBlock Text="Zoom" />
</Expander.Header>
<Expander.Content>
<Border Style="{StaticResource LightBorder}">
<StackPanel>
<TextBlock Text="Select Zoom Out Level" />
<ComboBox ItemsSource="{Binding ScaleFactorOptions}"
SelectedItem="{Binding SelectedScaleFactor, UpdateSourceTrigger=PropertyChanged}"
HorizontalAlignment="Left"
SelectionChanged="ZoomLevel_SelectionChanged"
>
<ComboBox.Resources>
<helper:ZoomConverter x:Key="ZoomConverter" />
</ComboBox.Resources>
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Converter={StaticResource ZoomConverter}}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</StackPanel>
</Border>
</Expander.Content>
</Expander>
<Expander IsExpanded="True" >
<Expander.Header>
<TextBlock Text="Time Period" />
</Expander.Header>
<Expander.Content>
<Border Style="{StaticResource LightBorder}">
<StackPanel>
<TextBlock Text="Change Time Period" />
<ComboBox ItemsSource="{Binding PeriodOptions}"
SelectedItem="{Binding SelectedPeriod, UpdateSourceTrigger=PropertyChanged}"
HorizontalAlignment="Left"
SelectionChanged="TimePeriod_SelectionChanged"
>
</ComboBox>
</StackPanel>
</Border>
</Expander.Content>
</Expander>
<Expander IsExpanded="True">
<Expander.Header>
<TextBlock Text="Properties" />
</Expander.Header>
<Expander.Content>
<Border Style="{StaticResource LightBorder}">
<StackPanel>
<TextBlock x:Name="txtProperty"/>
</StackPanel>
</Border>
</Expander.Content>
</Expander>
</StackPanel>
</Grid>
</Grid>
正如您在上面的代码中看到的那样,我已经注释掉了将宽度设置为 500 的行。当我这样做时,滚动条出现,我可以滚动查看可能与宽度一致或不一致的值我的画布。
我还尝试添加一个ColumnDefintion = Auto
只是为了看看这是否会有所帮助,但没有。
我是否必须在后面的代码中计算出 Canvas 的宽度,然后手动设置宽度,或者 XAML 是否可以通过使用属性Stretch
内HoriztonalAlignment
(或类似属性)自动为我执行此操作?