【问题标题】:WPF insert non english text into listviewWPF将非英文文本插入列表视图
【发布时间】:2018-12-29 06:16:51
【问题描述】:

所以我有简单的ListView 绑定:

查看模型:

ObservableCollection<ClipboardItem> Clipboards;

列表视图:

    <ListView Name="ListViewUsers"
              ItemsSource="{Binding Clipboards}"/>
<ListView.View>
                            <GridView >
                                <GridViewColumn>
                                    <GridViewColumn.CellTemplate>
                                        <DataTemplate>
                                            <Grid>
                                                <Grid>
                                                    <TextBlock Text="{Binding Path=Text}"
                                                               Foreground="{Binding Path=Foreground, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListViewItem}}}"
                                                               Margin="0,0,0,0"/>
                                                </Grid>
                                            </Grid>
                                        </DataTemplate>
                                    </GridViewColumn.CellTemplate>
                                </GridViewColumn>
                            </GridView>
                        </ListView.View>
    </ListView>

我所有的非英文文本 (Hebrew) 都是显示 as ???

有什么建议可以解决吗?

Edit

我添加了我的ClipboardItem 类:

public class ClipboardItem
    {
        public string Text { get; set; }
    }

我还添加了我的Clipboard 活动:

private void ClipboardMonitor_OnClipboardContentChanged(object sender, EventArgs e)
        {
            string clipboardText = Clipboard.GetText(TextDataFormat.Text);
            viewModel.Clipboards.Add(new ClipboardItem { Text = clipboardText });
        }

剪贴板监视器

public sealed class ClipboardMonitor : IDisposable
    {
        private static class NativeMethods
        {
            /// <summary>
            /// Places the given window in the system-maintained clipboard format listener list.
            /// </summary>
            [DllImport("user32.dll", SetLastError = true)]
            [return: MarshalAs(UnmanagedType.Bool)]
            public static extern bool AddClipboardFormatListener(IntPtr hwnd);

            /// <summary>
            /// Removes the given window from the system-maintained clipboard format listener list.
            /// </summary>
            [DllImport("user32.dll", SetLastError = true)]
            [return: MarshalAs(UnmanagedType.Bool)]
            public static extern bool RemoveClipboardFormatListener(IntPtr hwnd);

            /// <summary>
            /// Sent when the contents of the clipboard have changed.
            /// </summary>
            public const int WM_CLIPBOARDUPDATE = 0x031D;

            /// <summary>
            /// To find message-only windows, specify HWND_MESSAGE in the hwndParent parameter of the FindWindowEx function.
            /// </summary>
            public static IntPtr HWND_MESSAGE = new IntPtr(-3);
        }

        private HwndSource hwndSource = new HwndSource(0, 0, 0, 0, 0, 0, 0, null, NativeMethods.HWND_MESSAGE);

        public ClipboardMonitor()
        {
            hwndSource.AddHook(WndProc);
            NativeMethods.AddClipboardFormatListener(hwndSource.Handle);
        }

        public void Dispose()
        {
            NativeMethods.RemoveClipboardFormatListener(hwndSource.Handle);
            hwndSource.RemoveHook(WndProc);
            hwndSource.Dispose();
        }

        private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            if (msg == NativeMethods.WM_CLIPBOARDUPDATE)
            {
                OnClipboardContentChanged?.Invoke(this, EventArgs.Empty);
            }

            return IntPtr.Zero;
        }

        /// <summary>
        /// Occurs when the clipboard content changes.
        /// </summary>
        public event EventHandler OnClipboardContentChanged;
    }

编辑

private ClipboardMonitor clipboardMonitor;

public MainWindow()
        {
            InitializeComponent();
            InitiateClipboardMonitor();
        }

private void InitiateClipboardMonitor()
        {
            clipboardMonitor = new ClipboardMonitor();
            clipboardMonitor.OnClipboardContentChanged += ClipboardMonitor_OnClipboardContentChanged;
        }

【问题讨论】:

  • 我通过制作字符串类型的 ObservableCollection 的剪贴板列表,使用谷歌将英语翻译成希伯来语复制了文本。文字显示正确。您能否提供 ClipboardItem 类或任何将其放入 ClipboardItem 对象的功能。
  • 请看我的更新
  • 希伯来文文本在列表框中正确显示。是否有任何函数/方法可以在类对象中添加文本。我想这是造成问题的原因。你也可以删除结束标签 ItemsSource="{Binding Clipboards}"/>
  • 我看到有时当我从网页复制文本时,这个显示????出现有时不出现,是什么导致了这种奇怪的行为?
  • 我还添加了我的剪贴板事件,请查看我的更新

标签: wpf binding


【解决方案1】:

将 TextDataFormat.Text 更改为 TextDataFormat.UnicodeText

        private void ClipboardMonitor_OnClipboardContentChanged(object sender, EventArgs e)
    {
        string clipboardText = Clipboard.GetText(TextDataFormat.UnicodeText);
        Clipboards.Add(new ClipboardItem { Text = clipboardText });
    }

【讨论】:

  • @user979033 问题已重现,在将其更改为 UniCodeText 后,它对我有用。看看这个,让我知道是否还有案例。
  • 是的,现在看起来不错,我只是想不通为什么每个剪贴板副本都会导致相同的事件 3 次
  • @user979033 ClipboardMonitor_OnClipboardContentChanged 只调用一次。你可能不止一次指出它我猜使用委托
  • 是的,但仍然是 3 次相同的事件,我移动了所有代码,我的 ClipboardMonitor_OnClipboardContentChanged 函数现在是空的,仍然是 3 次
  • @user979033 你能否展示一下 ClipboardMonitor_OnClipboardContentChanged 事件是如何被订阅的?我已经把它放在了Window的类构造函数中
猜你喜欢
  • 1970-01-01
  • 2016-11-21
  • 2012-09-19
  • 1970-01-01
  • 2015-10-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-19
  • 2011-01-08
相关资源
最近更新 更多