none
嵌在WPF中的WinForm控件边框显示问题 RRS feed

  • 问题

  • 如题,使用WindowsFormsHost作为载体在WPF创建WinForm控件,但是出现了下述现象:

    代码如下:

    <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="auto" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
    
            <StackPanel
                Grid.Row="0"
                Margin="0,0,0,2"
                Background="{DynamicResource Gray}"
                Orientation="Horizontal">
                <StackPanel.Resources>
                    <Style TargetType="Line">
                        <Setter Property="X1" Value="0" />
                        <Setter Property="Y1" Value="3" />
                        <Setter Property="X2" Value="0" />
                        <Setter Property="Y2">
                            <Setter.Value>
                                <MultiBinding Converter="{StaticResource HeightConv}">
                                    <Binding Path="Y1" RelativeSource="{RelativeSource Mode=Self}" />
                                    <Binding Path="ActualHeight" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=StackPanel}" />
                                </MultiBinding>
                            </Setter.Value>
                        </Setter>
                        <Setter Property="Stroke" Value="{StaticResource RectColor}" />
                    </Style>
                </StackPanel.Resources>
                <Button
                    Content="&#xe606;"
                    Style="{StaticResource MyButton}"
                    ToolTip="Stop PlayBack" />
                <ToggleButton
                    Command="{Binding PlayCtrlCmd}"
                    IsChecked="{Binding IsPlaying, Mode=OneWay}"
                    ToolTip="Start or Pause">
                    <ToggleButton.Style>
                        <Style BasedOn="{StaticResource MyToggleButton}" TargetType="ToggleButton">
                            <Style.Triggers>
                                <Trigger Property="IsChecked" Value="True">
                                    <Setter Property="Content" Value="&#xe613;" />
                                </Trigger>
                                <Trigger Property="IsChecked" Value="False">
                                    <Setter Property="Content" Value="&#xe608;" />
                                </Trigger>
                            </Style.Triggers>
                        </Style>
                    </ToggleButton.Style>
                </ToggleButton>
                <Slider
                    Width="400"
                    VerticalAlignment="Center"
                    AutoToolTipPlacement="BottomRight"
                    IsSnapToTickEnabled="True"
                    LargeChange="1"
                    Maximum="{Binding FrameNum}"
                    Minimum="0"
                    SmallChange="1"
                    Value="{Binding CurrentFrame}">
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="ValueChanged">
                            <i:InvokeCommandAction Command="{Binding FrameChangedCmd}" />
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                </Slider>
                <Line Margin="5,0,5,0" />
                <Button
                    Command="{Binding OpenFileCmd}"
                    Content="&#xe628;"
                    FontSize="13"
                    Style="{StaticResource MyButton}" />
                <Line Margin="5,0,5,0" />
                <ToggleButton
                    Command="{Binding RecordCmd}"
                    CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=IsChecked}"
                    Content="REC"
                    Style="{StaticResource MyToggleButton}" />
                <Line Margin="5,0,5,0" />
                <TextBox
                    MaxHeight="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=StackPanel}, Path=ActualHeight}"
                    VerticalAlignment="Center"
                    BorderThickness="0"
                    IsEnabled="False"
                    Text="{Binding FilePath}"
                    TextWrapping="Wrap" />
            </StackPanel>
    
            <ContentControl Grid.Row="1" Background="{DynamicResource Gray}">
                <WindowsFormsHost Background="{DynamicResource Gray}" SnapsToDevicePixels="True">
                    <WinFormControl:Panel x:Name="host" />
                </WindowsFormsHost>
            </ContentControl>
        </Grid>

    然后我试了一下使用其他控件,例如Button或者TextBox,也发现了类似的问题:

    2020年7月30日 7:35

全部回复

  • 你好,

    我需要用你的代码去复现问题以供分析,所以你可以告诉我<MultiBinding Converter="{StaticResource HeightConv}"> 中 HeightConv的相关代码吗?

    谢谢

    Best regards

    Daisy Tian


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2020年7月31日 6:48
  • 你好,代码如下:

    只是做了一个高度转换,留了一个余量

    public class HeightConverter : IMultiValueConverter
        {
            public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
            {
                if (values.Length != 2)
                    return null;
                double margin = double.Parse(values[0].ToString());
                double height = double.Parse(values[1].ToString());
                return height - margin;
            }

            public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }

    2020年7月31日 8:28
  • 你好,

    我自己定义了你的MyButton和MyMyToggleButton,运行起来的界面很糟糕,所以我必须要用你的代码才能重现你的问题。你可以直接提供一个demo吗?这样可以加快我分析和解决你问题的速度,感谢你的理解。

    Best regards

    Daisy  Tian


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2020年8月3日 2:41