在园子里有没有做过这样控件的高手,在此先谢了,一起研究下!
这样一个问题,在WPF 下通过WindowsFormHost调用第三方用Winform 开发的控件,添加到WPF 界面的ListBox 中,可是当添加多个拉动ListBox 的滚动条的时候,WinForm 的控件跑出ListBox 的界面范围外。
这里是我的代码,我在WPF 中定义了一个DataTemplate ,在数据模板中通过WindowsFormHost 调用 第三方控件,如以下代码:
<Window.Resources> <DataTemplate x:Key="DataTemplate2"> <Grid x:Name="bubblegrid"> <Grid x:Name="bubble_left" VerticalAlignment="Center" RenderTransformOrigin="0.5,0.5" HorizontalAlignment="Left" Visibility="{Binding showleft}"> <Grid.Width> <System:Double>NaN</System:Double> </Grid.Width> <!--<Grid.RenderTransform> <CompositeTransform/> </Grid.RenderTransform>--> <Grid.ColumnDefinitions> <ColumnDefinition Width="23"/> <ColumnDefinition/> <ColumnDefinition Width="16"/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="13"/> <RowDefinition/> <RowDefinition Height="13"/> </Grid.RowDefinitions> <Grid x:Name="upper_left2" Height="13" Margin="0" Width="23"> <Grid.Background> <ImageBrush Stretch="Fill" ImageSource="image/4_03.png"/> </Grid.Background> </Grid> <Grid x:Name="upper_right2" Height="13" Margin="0" Width="16" Grid.Column="2"> <Grid.Background> <ImageBrush Stretch="Fill" ImageSource="image/4_01.png"/> </Grid.Background> </Grid> <Grid x:Name="lower_left2" Height="13" Margin="0" Width="23" Grid.Row="2"> <Grid.Background> <ImageBrush Stretch="Fill" ImageSource="image/4_09.png"/> </Grid.Background> </Grid> <Grid x:Name="lower_right2" Height="13" Margin="0" Width="16" Grid.Column="2" Grid.Row="2"> <Grid.Background> <ImageBrush Stretch="Fill" ImageSource="image/4_07.png"/> </Grid.Background> </Grid> <Grid x:Name="left2" Margin="0" Width="23" Grid.Row="1"> <Grid.Background> <ImageBrush ImageSource="image/4_06.png" Stretch="None"/> </Grid.Background> </Grid> <Grid x:Name="center2" Grid.Column="1" Margin="0" Grid.Row="1"> <Grid.Background> <ImageBrush Stretch="Fill" ImageSource="image/4fill.png"/> </Grid.Background> <WindowsFormsHost HorizontalAlignment="Center" MaxWidth="240" VerticalAlignment="Center"> <Msg:MyExtRichTextBox x:Name="msgbubble2" AutoSize="True"></Msg:MyExtRichTextBox> </WindowsFormsHost> </Grid> <Grid x:Name="right2" Margin="0" Width="16" Grid.Row="1" Grid.Column="2"> <Grid.Background> <ImageBrush ImageSource="image/4_04.png" Stretch="None"/> </Grid.Background> </Grid> <Grid x:Name="upper2" Grid.Column="1" Height="13" Margin="0"> <Grid.Background> <ImageBrush Stretch="Fill" ImageSource="image/4_02.png"/> </Grid.Background> </Grid> <Grid x:Name="lower2" Grid.Column="1" Height="13" Margin="0,-3,0,0" Grid.Row="2"> <Grid.Background> <ImageBrush Stretch="Fill" ImageSource="image/4_08.png"/> </Grid.Background> </Grid> </Grid> <Grid x:Name="LayoutRoot" Background="Transparent"/> </Grid> </DataTemplate> </Window.Resources> and call the DataTemplate here : <Grid x:Name="ContentPanel" Margin="0,40,254,0" Grid.Row="1"> <StackPanel> <ListBox x:Name="list" Margin="0" ItemTemplate="{StaticResource DataTemplate2}" Height="328" Background="White" Width="341"> <ListBox.ItemContainerStyle> <Style TargetType="ListBoxItem"> <Setter Property="HorizontalContentAlignment" Value="Stretch"/> </Style> </ListBox.ItemContainerStyle> </ListBox> </StackPanel> </Grid>
如图所示:
各位有研究过的,请指点下,先谢谢大家了!
Best Regards!
wpf里这种现象很多,它的设计跟web很像,我觉得你可以试试让winform的在添加item的是时候通知wpf它的新高度,然后让wpf重新绘制就好了。
谢谢这位兄台,我在研究直接用WPF 的控件来实现。