WPF插入非英文文本到列表视图

时间:2018-12-29 06:16:52

标签: wpf binding

所以我有简单的ListView带有绑定:

查看模型:

ObservableCollection<ClipboardItem> Clipboards;

ListView:

    <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 });
        }

ClipboardMonitor

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;
        }

1 个答案:

答案 0 :(得分:0)

将TextDataFormat.Text更改为TextDataFormat.UnicodeText

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