Tag: 自定义控件

WPF最佳实践:自定义控件是否适用于MVVM设计?

我正在寻找创建一个我可以在我的页面上重用的通用控件:一个AddressControl,它有Address1,Address2,City,State,Zip等… 最初我刚刚创建了一个包含所有这些项的类( AddressEntity )并实现了INotifyPropertyChanged 。 我在AddressControl的Code-Behind中将该类作为DependencyProperty包含在内,并将其用作DataContext以绑定其属性。 然后,有人说我的代码很难看,我应该看看MVVM。 看着它,我认为: AddressEntity.cs只是数据的容器(即Address1,Address2等)和成员(即Clone,ToString等) 我需要一些AddressViewModel来包装我的AddressEntity并提供PropertyNotification更改,validation等。 我需要以某种方式为此提供“视图”。 问题是我见过的每个例子都有一个UserControl作为View而不是CustomControl。 在我深入研究之前…… 是否可以在此示例中使用MVVM +自定义控件? 除了UserControl与CustomControl的主要区别之外,它与View几乎完全相同(UserControl vs CustomControl)吗? 基本上,我的CustomControl真的只是一个视图吗? 参考: WPF的Model-View-ViewModel(MVVM)设计模式

按钮的动画发光效果 – C#Windows窗体

我想按下按钮时将动画应用到我的按钮。 在WPF中,我可以使用Storyboard和触发器来创建动画。 这是一个例子: 和: Windows窗体没有Storyboard和触发器。 如何在Windows窗体中制作流畅的动画? 这是我的Windows窗体代码: void DelayTime() { timer = new Timer(); timer.Interval = (int)System.TimeSpan.FromSeconds(this.DelayTime).TotalMilliseconds; timer.Tick += (s, es) => { this.mouseover = false; this.Cursor = Cursors.Hand; this.Enabled = true; }; timer.Start(); } protected override void OnMouseDown(MouseEventArgs mevent) { base.OnMouseDown(mevent); mouseover = true; this.Enabled = false; DelayTime(); } protected override void OnPaint(PaintEventArgs e) […]

Dispatcher在Textchanged事件中的Messagebox.Show上抛出InvalidOperationException

首先,这是我的错误的错误日志条目 crash program @ 15-9-2011 15:01:30error:System.InvalidOperationException: Dispatcher processing has been suspended, but messages are still being processed. at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) 无论如何代码: private void TB_postcode_cijfers_TextChanged(object sender, TextChangedEventArgs e){ if (TB_postcode_cijfers.Text != string.Empty || TB_postcode_cijfers.Text.Length > 0) { LBL_postcode.Content = Postcode_cijfers + Postcode_letters; if (TB_postcode_cijfers.Text.Length == 4 && TB_postcode_letters.Text.Length […]

自定义控件 – 属性框中的可点击链接

我正在使用C#进行自定义控件,我需要添加一个属性框的链接(所以我可以在单击后显示一个表单)。 这是一个例子:

控件如何在该控件外部处理鼠标单击?

我正在编写一个自定义控件,我希望控件在用户点击控件时从编辑状态切换到正常状态。 我正在处理LostFocus事件,这有助于当用户标记或者点击另一个可聚焦的控件时。 但如果他们没有点击Focusable,它就不会切换出它的编辑状态。 所以我有两个解决方案: 当它进入编辑状态时,将树向上移动到最顶层元素,并为MouseDownEvent添加处理程序(并处理“已处理”事件)。 在处理程序中,我将控件从其编辑状态中踢出,并从最顶层的元素中删除处理程序。 这似乎有点像黑客,但它可能会运作良好。 示例代码: private void RegisterTopMostParentMouseClickEvent() { _topMostParent = this.FindLastVisualAncestor(); if ( _topMostParent == null ) return; _topMostParent.AddHandler( Mouse.MouseDownEvent, new MouseButtonEventHandler( CustomControlMouseDownEvent ), true ); } private void UnRegisterTopMostParentMouseClickEvent() { if ( _topMostParent == null ) return; _topMostParent.RemoveHandler( Mouse.MouseDownEvent, new MouseButtonEventHandler( CustomControlMouseDownEvent ) ); _topMostParent = null; } 使用Mouse.PreviewMouseDownOutsideCapturedElement并向我的控件添加处理程序。 在处理程序中,我将控件从其编辑状态中踢出。 […]

NumericUpDown with Unit | 定制控制| 场填充

我试图创建一个inheritanceNumericUpDown以显示可设置单位的自定义控件。 这是(视觉上)到目前为止我得到的: 我的代码: 看起来有点长,但不是那么做 class NumericUpDownUnit : NumericUpDown { public event EventHandler ValueChanged; /// /// Constructor creates a label /// public NumericUpDownUnit() { this.TextChanged += new EventHandler(TextChanged_Base); this.Maximum = 100000000000000000; this.DecimalPlaces = 5; this.Controls.Add(lblUnit); lblUnit.BringToFront(); UpdateUnit(); } public void TextChanged_Base(object sender, EventArgs e) { if(ValueChanged != null) { this.ValueChanged(sender, e); } } /// /// My […]

绘图时控制的翻转坐标

我在控件上绘制图形,但0,0位于控件的左上角。 有没有办法翻转坐标,使0,0位于控件的左下角?

根据停靠在其中的webBrowser控件中的数据调整自定义用户控件的大小

我有一个名为webBrowser1的webBrowser控件,它在自定义用户控件上添加并停靠为DockStyle.Full 。 Web浏览器动态接受一些HTML文本并显示它。 我禁用了webBrowser控件的滚动条。 我的问题是,只要内容有点冗长, webBrowser就会从下面隐藏它。 但是我的项目目标的要求是webBrowser不能显示滚动条或它不应该隐藏一些内容。 必须完全显示内容,而不滚动。 这意味着webBrowser停靠的用户控件必须根据webBrowser的内容调整自身大小。 所以,任何人都可以建议我如何实现这一目标? 我搜索了整个互联网,但没有发现任何东西。

绑定到DataConmplate for ItemsControl中的自定义控件

当我想绑定到我的自定义用户控件时,我遇到了基于ItemsControl定义的DataType DataTemplate绑定问题。 出于演示目的,我创建了简单的Item类示例,其中我有这样的项集合: public class Item { public string ItemNameToBeSureWhatPropertyIsBound { get; set; } } 在我的ViewModel中,我创建了这样的集合,并将其公开(有一个项目可以单独进行比较): public class MainWindowViewModel : INotifyPropertyChanged { private ObservableCollection _items; private Item _exampleItem; public MainWindowViewModel() { Items = new ObservableCollection(new[] { new Item { ItemNameToBeSureWhatPropertyIsBound = “Me” }, new Item { ItemNameToBeSureWhatPropertyIsBound = “MySelf” }, new Item { ItemNameToBeSureWhatPropertyIsBound = […]