Tag: wpf

C#获取当前光标图标

我一直试图这样做而且无法做到,这就是我所要做的: [DllImport(“user32.dll”)] static extern bool SetSystemCursor(IntPtr hcur, uint id); [DllImport(“user32.dll”, EntryPoint = “GetCursorInfo”)] public static extern bool GetCursorInfo(out CURSORINFO pci); [DllImport(“user32.dll”, EntryPoint = “CopyIcon”)] public static extern IntPtr CopyIcon(IntPtr hIcon); [StructLayout(LayoutKind.Sequential)] public struct CURSORINFO { public Int32 cbSize; // Specifies the size, in bytes, of the structure. public Int32 flags; // Specifies the cursor state. […]

RenderTargetBitmap内存泄漏

我试图使用RenderTargetBitmap渲染图像每次我从RenderTargetBitmap创建一个实例来渲染图像时内存增加,当完成时内存永远不会释放,这就是代码: RenderTargetBitmap rtb = new RenderTargetBitmap((int)(renderWidth * dpiX / 96.0), (int)(renderHeight * dpiY / 96.0), dpiX, dpiY, PixelFormats.Pbgra32); DrawingVisual dv = new DrawingVisual(); using (DrawingContext ctx = dv.RenderOpen()) { VisualBrush vb = new VisualBrush(target); ctx.DrawRectangle(vb, null, new System.Windows.Rect(new Point(0, 0), new Point(bounds.Width, bounds.Height))); } rtb.Render(dv); 请我帮忙如何释放内存并感谢所有人。

WPF切角元素

我试图在WPF中创建类似于下图的内容。 这个控件被设计成我应用程序中所有内容的基本视图,并且将位于具有背景的Window控件内(可能是某种渐变)。 要求如下: 三面圆角(左上角,左下角和右下角) 切断右侧的标签角,“切割区域”背后的背景是透明的,因此窗口的背景渐变显示(看起来它真的被剪掉了) 标题区域应该是一个内容容器,所以我可以在其中放置任何内容,如图标和文本 内容区域需要具有最小高度,然后在内部内容超过它时增长(不是动态 – 只需支持其中任何元素的高度) 我已经和我斗争了好几个小时,但对于WPF来说,我开始发现自己在圈子里跑来跑去。 我认为WPF的灵活性有很大的好处,但对于刚开始使用它的人来说几乎是太令人生畏了。 任何帮助将非常感激! 谢谢!

如何冻结无法冻结的freezable对象

在我的场景中,我想在我想在后台任务中渲染它之前冻结一个不变的BitmapCacheBrush。 不幸的是我收到错误“这个Freezable不能被冻结”。 是否有任何解决方法或hacky方式冻结也不是freezable对象? 也许可以通过reflection设置正确的属性来达到这个目标? 提前谢谢你们。 编辑:(我的示例代码请求) public static class ext { public static async Task RenderAsync(this Visual visual) { var bounds = VisualTreeHelper.GetDescendantBounds(visual); var bitmapCacheBrush = new BitmapCacheBrush(visual); bitmapCacheBrush.BitmapCache = new BitmapCache(); // We need to disconnect the visual here to make the freezable freezable :). Of course this will make our rendering blank //bitmapCacheBrush.Target […]

如何在XAML中创建类的实例?

我想创建没有可视元素的简单实用程序类,并在XAML中创建它,以便我可以定义数据绑定。 我尝试创建派生自DependencyObject类并在Window.Resources部分创建它,但它不调用任何构造函数。

PropertyGrid和动态对象类型

我正在编写一个GUI应用程序,我需要启用任意对象的编辑属性(它们的类型仅在运行时才知道)。 我决定使用PropertyGrid控件来启用此function。 我创建了以下类: [TypeConverter(typeof(ExpandableObjectConverter))] [DefaultPropertyAttribute(“Value”)] public class Wrapper { public Wrapper(object val) { m_Value = val; } private object m_Value; [NotifyParentPropertyAttribute(true)] [TypeConverter(typeof(ExpandableObjectConverter))] public object Value { get { return m_Value; } set { m_Value = value; } } } 当我得到一个我需要编辑的对象实例时,我为它创建了一个Wrapper并将其设置为所选对象: Wrapper wrap = new Wrapper(obj); propertyGrid.SelectedObject = wrap; 但是我遇到了以下问题 – 只有当obj的类型是某种自定义类型(即我自己定义的类,或者内置的复杂类型)时才能按预期工作,但是当obj是基元时则不行。 例如,如果我定义: [TypeConverter(typeof(ExpandableObjectConverter))] public class […]

WPF:App.xaml和Generic.xaml有什么区别?

请告诉我App.xaml和Generic.xaml之间的区别,我对这两者感到困惑!

Wpf – TabItem标题中的Contextmenu

我想在tabitem中添加一个contextmenu。 但只应通过单击选项卡的标题打开它。 我像这样添加了Contextmenu: ContextMenu _contextMenu; TabItem _tabItem; //Initialize the components _tabItem.ContextMenu = _contextMenu; 只有当您点击TabItem的标题时,才应打开ContextMenu。 但是如果你在tabItem的另一个位置点击,它就不应该显示出来。 我需要在运行时以编程方式执行此操作。 xaml中的解决方案也可以。

文本框转换发送密钥:Alt + Enter – > Enter

我希望能够修改wpf文本框的行为,而无需实现新类。 我想要一个像输入/ Alt + Enter行为的Excel,当用户点击“Enter”时文本框被validation(movefocus …),但当他点击“ALT + Enter”时,文本框必须添加一个新行(my textbox支持多行:AcceptsReturn为true)。 我已经尝试过(在文本框中的PreviewKeyDown事件): – 在此链接之后构建一个KeyEventArgs和一个TextCompositionEventArgs: 如何在C#中以编程方式生成keypress事件? – 我尝试过SendKeys.SendWait(“{ENTER}”)但它发送了许多新的行命令 有没有办法做到这一点 ? 谢谢 private void m_MeasurementName_PreviewKeyDown(object sender, KeyEventArgs e) { var tb = (sender as TextBox); if (Keyboard.Modifiers == ModifierKeys.Alt && Keyboard.IsKeyDown(Key.Enter)) { // 1st try var key = “\n\r”; var routedEvent = Keyboard.KeyDownEvent; tb.RaiseEvent(new TextCompositionEventArgs(InputManager.Current.PrimaryKeyboardDevice, new TextComposition(InputManager.Current, tb, key)) […]

在WPF中将IsEnabled绑定到IsCheck

我有CheckBox和RadioButton。 我需要将IsEnabled属性绑定到IsChecked属性 没有代码编写,有没有办法做到这一点?