Tag: custom controls

自定义控件中的依赖项属性意外地共享内存/值

我有以下设置: 自定义WPF控件(基类),派生自Canvas 该基类的实现 该实现的ObservableCollection依赖项属性 我有一个测试应用程序,显示我的自定义控件的三个唯一实例(例如 ,Test2,Test3等)。 当我运行并调试应用程序时, ObservableCollection的内容对于控件的所有三个实例都是相同的。 为什么是这样? 图表: [ContentProperty(“DataGroups”)] public abstract class Chart : Canvas { static Chart() { DefaultStyleKeyProperty.OverrideMetadata(typeof(Chart), new FrameworkPropertyMetadata(typeof(Chart))); } public ObservableCollection DataGroups { get { return (ObservableCollection)GetValue(DataGroupsProperty); } set { SetValue(DataGroupsProperty, value); } } public static readonly DependencyProperty DataGroupsProperty = DependencyProperty.Register(“DataGroups”, typeof(ObservableCollection), typeof(Chart), new FrameworkPropertyMetadata(new ObservableCollection(), FrameworkPropertyMetadataOptions.AffectsArrange)); public abstract […]

WPF自定义控件:集合类型的DependencyProperty

我有一个包含ListBox的CustomControl : 我使用Code Behind中的属性绑定ItemsSource : public partial class CustomList : UserControl, INotifyPropertyChanged { public CustomList( ) { InitializeComponent( ); } public ObservableCollection ListSource { get { return (ObservableCollection)GetValue( ListSourceProperty ); } set { base.SetValue(CustomList.ListSourceProperty, value); NotifyPropertyChanged( “ListSource” ); } } public static DependencyProperty ListSourceProperty = DependencyProperty.Register( “ListSource”, typeof( ObservableCollection ), typeof( CustomList ), new PropertyMetadata( […]

在WPF FlowDocument中嵌入自定义控件

有没有办法将自定义控件嵌入到flowdocument中并由FlowDocument查看器正确显示(将其导出到Xaml文本文件并由查看器打开)?

使用WPF C#中的多个控件组合创建自定义控件

我希望创建一个自定义控件,它应该是预定义控件的组合,如Textbox,Button,ListBox等, 请参考以下控件(只是一个样本) 我需要在单个自定义控件中组合控件。 当我按下按钮时,我需要在ListItem中添加Textbox值,最后我需要来自此控件的List。 预期的定制控制(只是一个样本) 描述 :我需要从用户那里获取字符串列表。 我添加了一个TextBox来获取用户的输入。 我添加了一个Button来在List添加文本。 为了显示List,我添加了一个ListBox。 我需要一个Single控件,它应该inheritance这三个控件。 在那我需要一个ItemsSource 双向绑定 。 如果List已更新,则应更新ItemSource。 我在超过15个地方使用这种结构。 所以,我希望将其作为自定义控件。 请帮助我如何将其作为单一控件实现? 我不需要用户控件,我需要一个自定义控件,请帮助我…… 项目源ViewModel集合甚至不更新 – 尽管ItemsSource具有值。

generic.xaml中的数据模板如何自动应用?

我有一个自定义控件,它有一个ContentPresenter,它将内容设置为任意对象。 此对象对其类型没有任何约束,因此我希望此控件基于应用程序或Generic.xaml中定义的数据模板定义的任何数据模板显示其内容。 如果在应用程序中我定义了一些数据模板(没有键,因为我希望它自动应用于该类型的对象),并且我使用绑定到该类型对象的自定义控件,则自动应用数据模板。 但我在generic.xaml中为某些类型定义了一些数据模板,我在其中定义了自定义控件样式,并且这些模板未自动应用。 这是generic.xaml: 如果我将“PredefinedType”类型的对象设置为contentpresenter中的内容,则不会应用数据模板。 但是,如果我在app.xaml中为使用自定义控件的应用程序定义数据模板,它是否有效。 有人有线索吗? 我真的不能假设控件的用户将定义这个数据模板,所以我需要一些方法将它与自定义控件联系起来。

DataGridTemplateColumn的自定义控件

我目前正在尝试从DataGridTemplateColumn创建一个自定义控件,该DataGridTemplateColumn将在我们的许多应用程序中重用。 我遇到了一些问题,在自定义控件上获取依赖属性以进行绑定并正确引发属性更改通知。 我目前有从DataGridTemplateColumninheritance的DataGridTemplateColumn ,xaml看起来像这样: 而背后的代码看起来像这样 public partial class DataGridDateColumn : DataGridTemplateColumn { public static readonly DependencyProperty SelectedDateProperty = DependencyProperty.Register(“SelectedDate”, typeof(DateTime?), typeof(DataGridDateColumn), new FrameworkPropertyMetadata(null, OnSelectedDateChanged)); private static void OnSelectedDateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { DataGridDateColumn col = (DataGridDateColumn)d; col.SelectedDate = (DateTime?)e.NewValue; } public DateTime? SelectedDate { get { return (DateTime?)GetValue(SelectedDateProperty); } set { SetValue(SelectedDateProperty, value); } } […]

绑定到工具提示的wpf自定义控件依赖项属性?

我有一个我在WPF中编写的自定义控件,它有一个布尔依赖属性: public static readonly DependencyProperty IsAlertProperty = DependencyProperty.Register(“IsAlert”, typeof(bool), typeof(AlertControl), new FrameworkPropertyMetadata(default(bool), FrameworkPropertyMetadataOptions.None, OnIsAlertChanged, null, false, UpdateSourceTrigger.PropertyChanged)); public bool IsAlert { get { return (bool)GetValue(IsAlertProperty); } set { SetValue(IsAlertProperty, value); } } 在我的Generic.xaml中,我有以下xaml代码: … 问题是这似乎不起作用。 我使用Snoop监视我的xaml,并且IsAlert属性正在适当地改变,但是如果我深入研究我的AlertControl.ToolTip ,我看到Visiblity DependencyProperty有一个绑定错误。 我也尝试使用RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:AlertControl}} ,但这也给出了绑定问题。 我不知道如何诊断这个,因为我的输出窗口也没有吐出任何绑定表达式错误。

C#:是否有必要在自定义控件中处理图形元素?

我创建了一个自定义控件,覆盖了它的paint事件。 当我尝试处理我创建的图形时,它们就会从屏幕上消失。 我不需要在自定义控件中使用dispose吗? 编辑:我已经包含了一个代码片段。 为什么我不能处理从PaintEventArgs创建的dc图形对象? 我需要处理它吗? class canvas : Control { PointF mouseDown; float newX; float newY; float zoomFactor = 1F; Graphics _dc; public canvas() { this.DoubleBuffered = true; mouseDown = new PointF(0F, 0F); this.Paint += new PaintEventHandler(ctrl_Paint); } private void ctrl_Paint(object sender, PaintEventArgs e) { Graphics dc = e.Graphics; _dc = dc; dc.SmoothingMode = […]

我的控件是“这里不允许的,因为它没有扩展类’System.Web.UI.UserControl’”

所以我还有另一个面条(对我而言)。 我正在尝试在CMS中创建自己的自定义控件我只有部分源代码(即供应商提供给我的样本)。 基本上我已经创建了一个名为DataDefinitionContent的类,它扩展了ControlBase 。 现在,从我从元数据中获取的内容, ControlBase扩展了UserControl ,所以我认为这将在没有戏剧性的情况下运行。 任何人都可以解释为什么这可能不适合我? 我的课: public partial class DataDefinitionContent : ControlBase, ICustomControl { … Stuff } ControlBase: using System; using System.Web.UI; namespace CMS.Web { public class ControlBase : UserControl { … Stuff } } 我的ascx文件: 我得到的错误: 分析器错误消息:此处不允许使用’CMS.Admin.WebUI.CustomControls.DataDefinitionContent,CoA.Admin.WebUI,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null’,因为它不扩展类’System.Web。 UI.UserControl”。 第1行:<%@ Control Language =“C#”AutoEventWireup =“true”CodeBehind =“DataDefinitionContent.ascx.cs”

如何创建带圆角的用户控件?

我正在尝试使用具有圆角的用户控件。 它没有固定的大小,但通常宽度不超过120像素。 我需要用户控件及其内容(标签和表格)具有圆形边缘,看起来像一个圆形框。 我用过这段代码。 [DllImport(“Gdi32.dll”, EntryPoint = “CreateRoundRectRgn”)] private static extern IntPtr CreateRoundRectRgn ( int nLeftRect, // x-coordinate of upper-left corner int nTopRect, // y-coordinate of upper-left corner int nRightRect, // x-coordinate of lower-right corner int nBottomRect, // y-coordinate of lower-right corner int nWidthEllipse, // height of ellipse int nHeightEllipse // width of ellipse ); […]