如何获取ListBox项高度

时间:2015-01-28 14:44:20

标签: c# wpf canvas listbox

我有一个ListBox

 <ListBox Name="listbox1"   ItemsSource="{Binding Area}" Grid.Column="0" Grid.Row="0"  HorizontalContentAlignment="Stretch">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Border BorderBrush="Green" BorderThickness="2" Margin="5" >
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="1*"/>
                                <RowDefinition Height="1*"/>
                                <RowDefinition Height="2*"/>
                            </Grid.RowDefinitions>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="2*" />
                                <ColumnDefinition Width="8*" />
                            </Grid.ColumnDefinitions>
                            <DockPanel  Grid.Row="0" Grid.Column="0" Margin="2">
                                <TextBox PreviewTextInput="minMaxPreviewInput" IsReadOnly="{Binding AutoChange}" Text="{Binding MinIntValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="30" DockPanel.Dock="Right" TextAlignment="Right"/>
                                <TextBlock Text="Minimum Value" DockPanel.Dock="Left"/>
                            </DockPanel>
                            <DockPanel  Grid.Row="1" Grid.Column="0" Margin="2">
                                <TextBox PreviewTextInput="minMaxPreviewInput" IsReadOnly="{Binding AutoChange}" Text="{Binding MaxIntValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="30" DockPanel.Dock="Right" TextAlignment="Right"/>
                                <TextBlock Text="Maximum Value" DockPanel.Dock="Left"/>
                            </DockPanel>
                            <DockPanel  Grid.Row="2" Grid.Column="0" Margin="2">
                                <TextBlock Text="{Binding AreaName}" FontSize="20" DockPanel.Dock="Left"/>
                                <TextBlock Text=":" FontSize="20" DockPanel.Dock="Left"/>
                                <TextBlock Text="{Binding Start}" FontSize="20" DockPanel.Dock="Left"/>
                                <CheckBox FlowDirection="RightToLeft" DockPanel.Dock="Right" IsChecked="{Binding AutoChange, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
                                    <TextBlock FlowDirection="LeftToRight" Text="Auto-Change:" />
                                </CheckBox>

                            </DockPanel>
                            <Border BorderBrush="Blue" BorderThickness="1" Grid.Column="1" Grid.RowSpan="3" Margin="5">
                                <Canvas Name="canvas1" >
                                    <Canvas.LayoutTransform>
                                        <ScaleTransform ScaleX="1" ScaleY="-1" CenterX=".5" CenterY=".5" />
                                    </Canvas.LayoutTransform>
                                    <Polyline Points="{Binding SegmentPoints}" Stroke="Black"    StrokeThickness="1"/>
                                </Canvas>
                            </Border>
                        </Grid>
                    </Border>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

一切都很有效。我在访问Canvas尺寸时遇到问题。我该如何访问它? Listbox.ActualHeightListBox.ActualWidth大于Canvas,我需要获得Canvas的确切大小。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

当您尝试将其他约束值的值基于Canvas大小时,请OneWayToSource上使用Binding模式ActualWidthActualHeight到ViewModel上的一些属性。

这会将控件中的值推送到ViewModel,允许您调整SegmentPoints中的值,以便根据Canvas大小正确缩放。