【发布时间】: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}"/>
-
我看到有时当我从网页复制文本时,这个显示????出现有时不出现,是什么导致了这种奇怪的行为?
-
我还添加了我的剪贴板事件,请查看我的更新