Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF Xaml,能够选择文本框和整个视图。同时能够复制文本框的内容_C#_Wpf_Xaml - Fatal编程技术网

C# WPF Xaml,能够选择文本框和整个视图。同时能够复制文本框的内容

C# WPF Xaml,能够选择文本框和整个视图。同时能够复制文本框的内容,c#,wpf,xaml,C#,Wpf,Xaml,我在列表视图中有一个文本框列表。当我点击文本框时,Listview选择不会改变。我必须单击文本框旁边的以选择listview项。我希望能够复制文本框的内容,同时也能够选择它 有没有办法做到这一点 <TextBox TextWrapping="WrapWithOverflow" Text="{Binding Path=Note, Mode=OneWay}" Width="512" MinWidth="512" M

我在列表视图中有一个文本框列表。当我点击文本框时,Listview选择不会改变。我必须单击文本框旁边的以选择listview项。我希望能够复制文本框的内容,同时也能够选择它

有没有办法做到这一点

    <TextBox 
           TextWrapping="WrapWithOverflow" 
           Text="{Binding Path=Note, Mode=OneWay}" 
           Width="512" MinWidth="512" MinHeight="70" 
           IsReadOnly="True"
           TextElement.FontWeight="DemiBold" Background="Transparent"    
           BorderThickness="0"
     />

这就是我目前所拥有的。

一张我正在谈论的图片

我真的很想只呆在xaml/wpf

编辑:listview的解决方案或以下任何人的答案

    <ListView.Resources>
         <Style TargetType="ListViewItem">
              <Style.Triggers>
                   <Trigger Property="IsKeyboardFocusWithin" Value="True"> <Setter Property="IsSelected" Value="True" />
                    </Trigger>
               </Style.Triggers>
           </Style>
        </ListView.Resources>

您的代码片段看起来不错。设置
IsReadOnly=“True”
应允许您选择和复制内容,但不允许编辑内容。但是,请注意,只有在选定的文本范围上单击鼠标右键时,关联菜单才会出现。这似乎是将文本框标记为只读的结果:如果“复制”、“剪切”和“粘贴”命令都不可用,它就不会显示菜单。”“复制”仅在右键单击选定内容时可用,“剪切”和“粘贴”仅在框可编辑时可用


我无法查看您的模型图像,因为Google Drive在此被阻止,但听起来您希望能够显示一条消息,同时允许用户突出显示整个消息并将其复制到剪贴板。在这种情况下,最快的解决方案可能是一个简单的自定义控件

下面的例子应该给你你想要的。复制到剪贴板应使用上下文菜单和Ctrl+C

MessagePane.cs:

using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Markup;

namespace WpfTest
{
    [ContentProperty("Message")]
    public class MessagePane : Control
    {
        public static readonly DependencyProperty TextWrappingProperty =
            TextBlock.TextWrappingProperty.AddOwner(
                typeof(MessagePane),
                new FrameworkPropertyMetadata(TextWrapping.WrapWithOverflow));

        public TextWrapping TextWrapping
        {
            get { return (TextWrapping)GetValue(TextWrappingProperty); }
            set { SetValue(TextWrappingProperty, value); }
        }

        public static readonly DependencyProperty MessageProperty =
            DependencyProperty.Register(
                "Message",
                typeof(string),
                typeof(MessagePane),
                new PropertyMetadata(default(string)));

        public string Message
        {
            get { return (string)GetValue(MessageProperty); }
            set { SetValue(MessageProperty, value); }
        }

        static MessagePane()
        {
            DefaultStyleKeyProperty.OverrideMetadata(
                typeof(MessagePane),
                new FrameworkPropertyMetadata(typeof(MessagePane)));

            CommandManager.RegisterClassCommandBinding(
                typeof(MessagePane),
                new CommandBinding(
                    ApplicationCommands.Copy,
                    (sender, args) =>
                    {
                        var messagePane = sender as MessagePane;
                        if (messagePane != null)
                            Clipboard.SetText(messagePane.Message);
                    }));
        }

        protected override void OnPreviewMouseDown(MouseButtonEventArgs e)
        {
            this.Focus();
            base.OnPreviewMouseDown(e);
        }
    }
}
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:l="clr-namespace:WpfTest">
  <Style TargetType="l:MessagePane">
    <Setter Property="Background"
            Value="Transparent" />
    <Setter Property="TextWrapping"
            Value="WrapWithOverflow" />
    <Setter Property="Padding"
            Value="3,2" />
    <Setter Property="Focusable"
            Value="True" />
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="l:MessagePane">
          <Border x:Name="MessageBorder"
                  Background="{TemplateBinding Background}"
                  BorderBrush="{TemplateBinding BorderBrush}"
                  BorderThickness="{TemplateBinding BorderThickness}"
                  Padding="{TemplateBinding Padding}">
            <Border.ContextMenu>
              <ContextMenu>
                <MenuItem Command="{x:Static ApplicationCommands.Copy}" />
              </ContextMenu>
            </Border.ContextMenu>
            <TextBlock TextWrapping="{TemplateBinding TextWrapping}"
                       Text="{TemplateBinding Message}" />
          </Border>
          <ControlTemplate.Triggers>
            <Trigger Property="IsFocused"
                     Value="True">
              <Setter TargetName="MessageBorder"
                      Property="Background"
                      Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
              <Setter TargetName="MessageBorder"
                      Property="BorderBrush"
                      Value="{DynamicResource {x:Static SystemColors.ActiveBorderBrushKey}}" />
            </Trigger>
          </ControlTemplate.Triggers>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>
</ResourceDictionary>
<StackPanel Orientation="Vertical">
  <l:MessagePane Message="Lorem ipsum dolor sit amet ..." />
  <l:MessagePane Message="Sed eu sem egestas, lobortis orci at..." />
  <l:MessagePane Message="Vivamus in turpis metus ..." />
</StackPanel>
Themes\Generic.xaml:

using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Markup;

namespace WpfTest
{
    [ContentProperty("Message")]
    public class MessagePane : Control
    {
        public static readonly DependencyProperty TextWrappingProperty =
            TextBlock.TextWrappingProperty.AddOwner(
                typeof(MessagePane),
                new FrameworkPropertyMetadata(TextWrapping.WrapWithOverflow));

        public TextWrapping TextWrapping
        {
            get { return (TextWrapping)GetValue(TextWrappingProperty); }
            set { SetValue(TextWrappingProperty, value); }
        }

        public static readonly DependencyProperty MessageProperty =
            DependencyProperty.Register(
                "Message",
                typeof(string),
                typeof(MessagePane),
                new PropertyMetadata(default(string)));

        public string Message
        {
            get { return (string)GetValue(MessageProperty); }
            set { SetValue(MessageProperty, value); }
        }

        static MessagePane()
        {
            DefaultStyleKeyProperty.OverrideMetadata(
                typeof(MessagePane),
                new FrameworkPropertyMetadata(typeof(MessagePane)));

            CommandManager.RegisterClassCommandBinding(
                typeof(MessagePane),
                new CommandBinding(
                    ApplicationCommands.Copy,
                    (sender, args) =>
                    {
                        var messagePane = sender as MessagePane;
                        if (messagePane != null)
                            Clipboard.SetText(messagePane.Message);
                    }));
        }

        protected override void OnPreviewMouseDown(MouseButtonEventArgs e)
        {
            this.Focus();
            base.OnPreviewMouseDown(e);
        }
    }
}
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:l="clr-namespace:WpfTest">
  <Style TargetType="l:MessagePane">
    <Setter Property="Background"
            Value="Transparent" />
    <Setter Property="TextWrapping"
            Value="WrapWithOverflow" />
    <Setter Property="Padding"
            Value="3,2" />
    <Setter Property="Focusable"
            Value="True" />
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="l:MessagePane">
          <Border x:Name="MessageBorder"
                  Background="{TemplateBinding Background}"
                  BorderBrush="{TemplateBinding BorderBrush}"
                  BorderThickness="{TemplateBinding BorderThickness}"
                  Padding="{TemplateBinding Padding}">
            <Border.ContextMenu>
              <ContextMenu>
                <MenuItem Command="{x:Static ApplicationCommands.Copy}" />
              </ContextMenu>
            </Border.ContextMenu>
            <TextBlock TextWrapping="{TemplateBinding TextWrapping}"
                       Text="{TemplateBinding Message}" />
          </Border>
          <ControlTemplate.Triggers>
            <Trigger Property="IsFocused"
                     Value="True">
              <Setter TargetName="MessageBorder"
                      Property="Background"
                      Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
              <Setter TargetName="MessageBorder"
                      Property="BorderBrush"
                      Value="{DynamicResource {x:Static SystemColors.ActiveBorderBrushKey}}" />
            </Trigger>
          </ControlTemplate.Triggers>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>
</ResourceDictionary>
<StackPanel Orientation="Vertical">
  <l:MessagePane Message="Lorem ipsum dolor sit amet ..." />
  <l:MessagePane Message="Sed eu sem egestas, lobortis orci at..." />
  <l:MessagePane Message="Vivamus in turpis metus ..." />
</StackPanel>
用法示例:

using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Markup;

namespace WpfTest
{
    [ContentProperty("Message")]
    public class MessagePane : Control
    {
        public static readonly DependencyProperty TextWrappingProperty =
            TextBlock.TextWrappingProperty.AddOwner(
                typeof(MessagePane),
                new FrameworkPropertyMetadata(TextWrapping.WrapWithOverflow));

        public TextWrapping TextWrapping
        {
            get { return (TextWrapping)GetValue(TextWrappingProperty); }
            set { SetValue(TextWrappingProperty, value); }
        }

        public static readonly DependencyProperty MessageProperty =
            DependencyProperty.Register(
                "Message",
                typeof(string),
                typeof(MessagePane),
                new PropertyMetadata(default(string)));

        public string Message
        {
            get { return (string)GetValue(MessageProperty); }
            set { SetValue(MessageProperty, value); }
        }

        static MessagePane()
        {
            DefaultStyleKeyProperty.OverrideMetadata(
                typeof(MessagePane),
                new FrameworkPropertyMetadata(typeof(MessagePane)));

            CommandManager.RegisterClassCommandBinding(
                typeof(MessagePane),
                new CommandBinding(
                    ApplicationCommands.Copy,
                    (sender, args) =>
                    {
                        var messagePane = sender as MessagePane;
                        if (messagePane != null)
                            Clipboard.SetText(messagePane.Message);
                    }));
        }

        protected override void OnPreviewMouseDown(MouseButtonEventArgs e)
        {
            this.Focus();
            base.OnPreviewMouseDown(e);
        }
    }
}
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:l="clr-namespace:WpfTest">
  <Style TargetType="l:MessagePane">
    <Setter Property="Background"
            Value="Transparent" />
    <Setter Property="TextWrapping"
            Value="WrapWithOverflow" />
    <Setter Property="Padding"
            Value="3,2" />
    <Setter Property="Focusable"
            Value="True" />
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="l:MessagePane">
          <Border x:Name="MessageBorder"
                  Background="{TemplateBinding Background}"
                  BorderBrush="{TemplateBinding BorderBrush}"
                  BorderThickness="{TemplateBinding BorderThickness}"
                  Padding="{TemplateBinding Padding}">
            <Border.ContextMenu>
              <ContextMenu>
                <MenuItem Command="{x:Static ApplicationCommands.Copy}" />
              </ContextMenu>
            </Border.ContextMenu>
            <TextBlock TextWrapping="{TemplateBinding TextWrapping}"
                       Text="{TemplateBinding Message}" />
          </Border>
          <ControlTemplate.Triggers>
            <Trigger Property="IsFocused"
                     Value="True">
              <Setter TargetName="MessageBorder"
                      Property="Background"
                      Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
              <Setter TargetName="MessageBorder"
                      Property="BorderBrush"
                      Value="{DynamicResource {x:Static SystemColors.ActiveBorderBrushKey}}" />
            </Trigger>
          </ControlTemplate.Triggers>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>
</ResourceDictionary>
<StackPanel Orientation="Vertical">
  <l:MessagePane Message="Lorem ipsum dolor sit amet ..." />
  <l:MessagePane Message="Sed eu sem egestas, lobortis orci at..." />
  <l:MessagePane Message="Vivamus in turpis metus ..." />
</StackPanel>
代码隐藏中的命令处理程序:

private void OnCopyCommandExecuted(object sender, ExecutedRoutedEventArgs e)
{
    var message = e.Parameter as string;
    if (message != null)
        Clipboard.SetText(message);
}

我不明白,这是默认行为,即使使用您的代码,您也应该能够做到这一点。另外,你的标题看起来与问题无关。我上传这张图片是为了更好地展示我的问题。我希望能够突出显示消息内容,但它不会选择它周围的整个选项卡,因为它是一个文本框。如果我先单击外部,它会高亮显示整个选项卡,然后我可以高亮显示内容@杰克:你需要用简单明了的英语描述“整体视图”对你个人意味着什么。除了你,没人知道你说的那句话是什么意思。你需要解释一下。您需要详细解释用户界面“视图”的哪一部分。您需要对此进行解释,以便我能够准确地说出哪些控件的哪些部分是“视图”的一部分,哪些控件的哪些部分不是“视图”的一部分。如果这是一个很好的解释,我应该能够在你的屏幕截图中选择任何给定的像素,并告诉你它是在“视图”内还是在“视图”外。你需要对“选择”做同样的操作。我不需要编辑消息,但我只想能够突出显示消息本身。但我也希望显示器的其余部分能够高亮显示。我不确定我描述的是否足够。对,这就是我从你的帖子中推断出来的:你希望能够选择和复制内容,但不能编辑它。这应该是书面的,还有我提到的警告。@JakeYoung FWIW这个描述对我来说毫无意义:什么是“剩余的显示”?也许你需要做线框,详细描述你希望用户采取的步骤,以及你的用户界面应该做什么来回应:“用户右键单击第一条消息的第二个单词,弹出上下文菜单,其中显示“复制并选择所有命令”——这是一种特殊性。这是我拍的一张照片。我希望能够突出显示消息的内容,但我想因为它是一个文本框,所以它不会选择整个视图。@Jake您的问题的补充内容应该放在您的问题中。因此,我们对非现场资源的链接持悲观看法。我们经常发现这些联系在时间上消失了,使问题变得不连贯。注意,请再次阅读我之前的评论。当人们告诉你你的描述太模糊时,请不要引入新的模糊术语。我无法知道“选择整个视图”对您意味着什么。不可能。