【问题标题】:Unable to select text in WPF TextBox inside a Popup无法在弹出窗口内的 WPF 文本框中选择文本
【发布时间】:2011-02-19 20:59:27
【问题描述】:

我正在尝试在状态信息弹出窗口中托管一个多行文本框,以显示只读、多行、可滚动的信息。以下 XAML 运行良好,除了文本不可选择(因此用户可以复制它)。

<!-- Status info popup -->
<Popup AllowsTransparency="True" PopupAnimation="Fade" Placement="Center" StaysOpen="False"
       PlacementTarget="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type v:ModuleView}}}" 
       IsOpen="{Binding ShowingStatusInformation}">
    <Border CornerRadius="5">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>

            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>

            <TextBlock Text="Status Information"
                       Grid.Column="0" Grid.Row="0" VerticalAlignment="Center" />                        
            <Button Content="OK" IsDefault="True" Command="{Binding ToggleStatusInfoCommand}"
                    HorizontalAlignment="Right" Margin="0 5" Padding="20 3" 
                    Grid.Column="1" Grid.Row="0" VerticalAlignment="Center">
                <Button.CommandParameter><sys:Boolean>False</sys:Boolean></Button.CommandParameter>
            </Button>

            <TextBox IsReadOnly="True" Text="{Binding StatusInformation}" 
                     Margin="6 6 6 3" Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="1"
                     TextWrapping="Wrap" VerticalScrollBarVisibility="Auto" 
                     MaxHeight="300" />
        </Grid>
    </Border>
</Popup>

视图模型上的对应属性:

    public string StatusInformation
    {
        get { return _statusInformation; }
        set
        {
            _statusInformation = value;
            _propertyChangedHelper.NotifyPropertyChanged(this, () => StatusInformation);
        }
    }

    public bool ShowingStatusInformation
    {
        get { return _showingStatusInformation; }
        set
        {
            _showingStatusInformation = value;
            _propertyChangedHelper.NotifyPropertyChanged(this, () => ShowingStatusInformation);
        }
    }

在弹出窗口中托管文本框是否会以某种方式禁用文本选择,还是我的绑定有问题?我正在替换一个模式窗口中托管的文本框,其中文本是可选的。

更新:这发生在 .NET 3.5 应用程序中,WPF 托管在 Win Forms 容器中。

【问题讨论】:

  • 我将此代码粘贴到使用 WPF 4 的项目中,我可以在 TextBox 中选择文本。
  • 这发生在 .NET 3.5 项目中,WPF 托管在 Win Forms 容器中。
  • @Zamboni:感谢您在 WPF 4 中尝试。它缩小了问题范围。
  • 它可以在独立的 3.5 WPF 项目中工作吗?我想知道它是否与winforms容器有关。
  • 它在 3.5SP1 独立项目中运行良好...@Tom,您可能走在正确的轨道上。

标签: wpf select textbox popup


【解决方案1】:

您的控件在什么时候被实例化 - 在 winforms 控件的构造函数中还是稍后?也许你可以试试 Loaded 或 ControlCreated。

这听起来有点像 ElementHost.EnableModelessKeyboardInterop 没有被调用时发生的事情,但它不能通过弹出窗口调用。

一种解决方法可能是添加一个“复制”按钮...

【讨论】: