Tag: 用户控件

有没有办法检测用户控件外的鼠标点击?

我正在创建一个自定义下拉框,我想在下拉框外单击鼠标时进行注册,以便隐藏它。 是否可以检测到控件外的点击? 或者我应该在包含的表单上制作一些机制,并在任何下拉框打开时检查鼠标点击?

将子控件的click事件传递给父控件

我有一个Windows窗体,有一个窗格,其中包含另一个派生自Windows窗体的类。 它包含在窗格中作为控件。 它本身包含两个按钮。 我希望子控件的事件一直传递到父窗口。 例如,窗格中的子窗口有一个Cancel按钮,该按钮应该关闭它。 我想要父控件,也就是关闭主窗口,但是如何拦截子控件的按钮单击事件? 我可以修改子控件,但只有在没有其他方法以正确的方式实现这一点时,我宁愿避免它。

在WPF UserControl中附加ICommand

我实现了一个带有图像的简单按钮: 如您所见,我公开了一个ButtonCommand属性,以便能够将ICommand附加到此UserControl: public partial class ImageButton : UserControl { /// /// The dependency property that gets or sets the source of the image to render. /// public static DependencyProperty ImageSourceProperty = DependencyProperty.Register(“ButtonImage”, typeof(ImageSource), typeof(ImageButton)); public static DependencyProperty TextProperty = DependencyProperty.Register(“ButtonText”, typeof(string), typeof(ImageButton)); public static DependencyProperty ButtonCommandProperty = DependencyProperty.Register(“ButtonCommand”, typeof(ICommand), typeof(ImageButton)); public ImageButton() { this.DataContext = […]

是否可以在.Net 3.5中进行通用控制?

我声明了以下Generic usercontrol: public partial class MessageBase : UserControl { protected T myEntry; public MessageBase() { InitializeComponent(); } public MessageBase(T newEntry) { InitializeComponent(); myEntry = newEntry; } } } 但编译器不允许我这样做: public partial class MessageControl : MessageBase { public MessageControl() { InitializeComponent(); } } 如何在C#中创建通用用户控件?

Winforms用户控制自定义事件

有没有办法提供用户控件自定义事件,并在用户控件中的事件上调用事件。 (我不确定调用是否是正确的术语) public partial class Sample: UserControl { public Sample() { InitializeComponent(); } private void TextBox_Validated(object sender, EventArgs e) { // invoke UserControl event here } } 和MainForm: public partial class MainForm : Form { private Sample sampleUserControl = new Sample(); public MainForm() { this.InitializeComponent(); sampleUserControl.Click += new EventHandler(this.CustomEvent_Handler); } private void CustomEvent_Handler(object sender, EventArgs […]

将子项添加到UserControl

我的任务 创建一个UserControl ,它应该能够包含WPF中可用的任何可视子对象,子对象显示在一个容器中,该容器是UserControl的子对象。 我的问题 我无法设法让孩子们在我的容器中正确显示,我尝试了serval方式,并没有找到适合设计师的方法。 我也尝试使用ContentControl但没有显示任何内容。 我的方法 首先,我找到了这个链接,我尝试了一些变化。 我设法在正确的容器中显示内容,但它在设计器中不起作用,因为content-property是set-private而设计者想要覆盖它。 将所有内容放在XAML中都有效但与设计人员合作时这并不好。 这可能是最喜欢的方式。 在此之后,我试图通过将Content -property绑定到UIElementCollection -type的可绑定属性来使用ContentControl 。 这种方法不会给设计师带来任何错误,但我不得不承认我从未在容器中看到任何控件(例如Button )。 它保持空白但添加了孩子。 结论 经过一段时间寻找一个简单快捷的解决方案后,我决定在这里寻求解决方案。 我有点失望。 如果微软能够获得MSDN样本,那将非常有用。 我确信必须有一种简单的方法来存档。 现在的情况 感谢Andrei Gavrila和jberger我存档创建一个显示内容的节点(参见下面的代码),但仍有两个问题: – 没有设计师支持 – 边框(参见xaml)未在设计器中显示,并且在应用程序时未显示正在运行甚至没有保证金 public class NodeContent : ContentControl { static NodeContent() { DefaultStyleKeyProperty.OverrideMetadata(typeof(NodeContent), new FrameworkPropertyMetadata(typeof(NodeContent))); } } public partial class Node : UserControl, INotifyPropertyChanged { UIElementCollection _Elements; public […]

在回发时保持动态添加用户控件的状态?

我有一个只包含文本框的用户控件,在另一个表单上我动态添加了这个用户控件,用户可以多次添加用户控件。 我使用会话变量来重新创建用户控件(也许这种方法听起来不太酷)。 重新创建控件后,文本框的值显然会消失。 是否有任何解决方案来维护用户控件的回发状态?

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

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

在DataContext中使用时无法删除文件

我的应用程序在屏幕上显示图像(基于本地计算机上的文件的图像),用户可以根据需要删除它们。 每次我尝试删除文件时,都会导致以下错误消息: “The process cannot access the file ‘C:\\Users\\Dave\\Desktop\\Duplicate\\Swim.JPG’ because it is being used by another process.” 我理解错误信息。 我有一个UserControl接受文件路径(通过构造函数中的参数),然后将其绑定到它的(UserControl) DataContext 。 作为调试此问题的一部分,我发现问题是由于在UserControl中设置DataContext。 如果我删除this.DataContext = this; 从我的UserControl中,然后我可以删除该文件。 所以,我的TestUnit看起来像 Ui.UserControls.ImageControl ic = new ImageControl( @”C:\Users\Dave\Desktop\Duplicate\Swim.JPG”); try { File.Delete(@”C:\Users\Dave\Desktop\Duplicate\Swim.JPG”); } catch (Exception ex) { Assert.Fail(ex.Message); } UserControl CodeBehind public ImageControl(string path) { this.FilePath = path; this.DataContext = this; […]

脱发和MVVM用户控件

我有一个使用MVVM模式用C#和WPF编写的用户控件。 我想要做的就是在绑定的ViewModel中有一个属性,该属性暴露在控件之外。 我希望能够绑定到它,并且我希望对绑定到公开值的控件之外的任何内容进行对属性的任何更改。 这听起来很简单,但它让我拉出我的头发(并没有留下太多的东西)。 我在用户控件中有一个依赖属性。 ViewModel具有实现INotifyPropertyChanged接口的属性,并正确调用PropertyChanged事件。 一些问题:1)如何获取ViewModel属性的更改并将其绑定到依赖属性而不破坏MVVM分离? 到目前为止,我设法做到这一点的唯一方法是在后面的Controls代码中分配ViewModels PropertyChanged事件,这绝对不是MVVM。 2)使用上面的软糖,我可以获得Dependency属性来启动它的PropertyChangedCallback,但是在控件之外绑定到它的任何东西都不会获​​得更改。 必须有一种简单的方法来完成所有这些。 请注意,我没有在这里发布任何代码 – 我希望不用我现有的代码来影响答案。 而且,你可能都会嘲笑它… 抢 好的,澄清 – 代码示例: usercontrol代码背后: public static DependencyProperty NewRepositoryRunProperty = DependencyProperty.Register(“NewRepositoryRun”, typeof(int?), typeof(GroupTree), new FrameworkPropertyMetadata( null, new PropertyChangedCallback(OnNewRepositoryRunChanged))); public int? NewRepositoryRun { get { return (int?)GetValue(NewRepositoryRunProperty); } set { SetValue(NewRepositoryRunProperty, value); } } private static void OnNewRepositoryRunChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) […]