Tag: wpf

如何访问ItemsControl的子项?

如果我有一个从ItemsControl派生的组件,我可以访问它的ItemsControl ,以便我可以循环它们来执行某些操作吗? 我现在似乎找不到任何简单的方法。

WPF命令路由行为的不一致性取决于UI焦点状态

我有一个RoutedUICommand命令,可以通过两种不同的方式触发: 直接通过ICommand.Execute点击按钮事件; 使用声明性语法: 。 该命令仅由顶部窗口处理: 第一种方法仅在窗口中存在聚焦元素时才有效。 无论焦点如何,第二个总是这样。 我查看了BCL的ICommand.Execute实现,发现如果Keyboard.FocusedElement为null ,命令不会被触发,所以这是设计的。 我仍然质疑,因为顶层可能有一个处理程序(就像在我的情况下)仍然希望接收命令,即使应用程序没有UI焦点(例如,我可能想调用ICommand.Execute当收到套接字消息时,从异步任务中执行)。 让它成为现实,我仍然不清楚为什么第二种(声明性)方法总是有效,无论焦点状态如何。 我对WPF命令路由的理解中缺少什么? 我敢肯定这不是一个错误,而是一个function。 以下是代码。 如果你喜欢玩它,这是完整的项目 。 单击第一个按钮 – 命令将被执行,因为焦点位于TextBox内部。 点击第二个按钮 – 一切都很好。 单击“ Clear Focus按钮。 现在第一个按钮( ICommand.Execute )不执行命令,而第二个按钮仍然执行。 您需要单击TextBox以使第一个按钮再次工作,因此有一个聚焦元素。 这是一个人为的例子,但它具有现实生活中的意义。 我将发布一个关于使用WindowsFormsHost托管WinForms控件的相关问题( 此处询问 [EDITED] ),在这种情况下,当焦点位于WindowsFormsHost内时, Keyboard.FocusedElement始终为null (通过ICommand.Execute有效地终止命令执行)。 XAML代码: C#代码 ,大部分与焦点状态记录有关: using System; using System.Windows; using System.Windows.Input; namespace WpfCommandTest { public partial class MainWindow : Window { […]

如何将WPF英寸单位转换为Winforms像素,反之亦然?

我有一个在WPF设计的窗口,我在WinForms所有者的中心使用它。 现在,我想移动所有者表单,此时我的WPF窗口也要移动到表单的中心! 但我有一个问题,只有当窗口位于屏幕中心形成的窗体的中心时。 否则以不同于Windows坐标的forms运行。 我只是将窗体的位移值添加到窗口位置。 现在我得出结论,WPF Windows上像素的坐标因WinForms而不同! 如何将WPF窗口位置转换为WinForms基本位置,反之亦然? 所有者表格代码是: public partial class Form1 : Form { private WPF_Window.WPF win; public Form1() { InitializeComponent(); win = new WPF(); win.Show(); CenterToParent(win); } private void CenterToParent(System.Windows.Window win) { win.Left = this.Left + (this.Width – win.Width) / 2; win.Top = this.Top + (this.Height – win.Height) / 2; } protected […]

避免从multithreadingc#MVVM应用程序中的ViewModel对象调用BeginInvoke()

我的C#应用​​程序有一个数据提供程序组件,它在自己的线程中异步更新。 ViewModel类都inheritance自实现INotifyPropertyChanged的基类。 为了让异步数据提供程序使用PropertyChanged事件更新View中的属性,我发现我的ViewModel与视图紧密耦合,因为只需要从GUI线程中引发事件! #region INotifyPropertyChanged /// /// Raised when a property on this object has a new value. /// public event PropertyChangedEventHandler PropertyChanged; /// /// Raises this object’s PropertyChanged event. /// /// The property that has a new value. protected void OnPropertyChanged(String propertyName) { PropertyChangedEventHandler RaisePropertyChangedEvent = PropertyChanged; if (RaisePropertyChangedEvent!= null) { var propertyChangedEventArgs = […]

如何在ObservableCollection上引发CollectionChanged事件,并将更改的项目传递给它?

我有一个inheritance自ObservableCollection的类,并添加了一些其他方法,如AddRange和RemoveRange 我的基本方法调用是这样的: public void AddRange(IEnumerable collection) { foreach (var i in collection) Items.Add(i); OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); } 我的问题是我想访问CollectionChanged事件中的e.NewItems或e.OldItems来对CollectionChanged任何项目执行操作,并且NotifyCollectionChangedAction.Reset操作不传递这些值 void Instances_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { if (e.NewItems != null) // e.NewItems is always null { foreach (var item in e.NewItems) { if (item is EventInstanceModel) ((EventInstanceModel)item).ParentEvent = this; } } } 所以我想我可以使用NotifyCollectionChangedAction.Add而不是Reset ,但是抛出一个Range actions are not supportedexception […]

从delphi2006调用.net dll来显示wpf表单

我正在使用Robert Gieseckes伟大的Unmanaged Exports从Delphi2006调用ac#-Dll。 如果我使用输入和输出的简单过程和函数,一切都很好。 但现在我想通过调用OpenMyWindow()显示一个Wpf-Window。 在这里,我得到一个“外部例外E0434352”。 我不知道为什么这不起作用。 无论如何,我认为这与wpf方面的初始化有关。 这是Delphi代码: unit Unit2; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, Menus, StdCtrls; type TForm2 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); end; procedure OpenMyWindow(); stdcall; external ‘ClassLibraryToDelphi.dll’; var Form2: TForm2; implementation {$R *.dfm} procedure TForm2.Button1Click(Sender: TObject); begin OpenMyWindow(); end; end. 现在是c#部分(它是一个将UserControl改为窗口的ClassLibrary): using System.Linq; […]

VS 2010将非GUI类文件设置为Component

我有一个烦恼已经发生了很长一段时间与Visual Studio 2010.我有一个类文件,我做了VS保存为类型“组件”无缘无故我能辨别。 如果我忘记并尝试打开文件,它会查找不存在的设计器。 我查看了Google并发现了VS 2005的一些类似问题,但问题似乎与从GUI组件类(listbox,combobox等)派生有关。 这堂课不这样做。 该文件是GpsUtilities.cs 。 它出现在csproj文件中,如下所示, SubType为Component 。 没有其他对该文件的引用,即没有任何声明它作为DependentUpon 。 Component 即使我删除了SubType标记,即使我明确地将其设置为Code而不是Component ,它仍然将其保存为Component SubType 。 这是类结构(所有代码都被删除)。 正如我所说,它不会inheritance,甚至不会导入与GUI相关的任何名称空间。 using System; using System.ComponentModel; using System.IO.Ports; using System.Text.RegularExpressions; using System.Timers; using System.Xml.Serialization; namespace AppNamespace { public class GpsUtil : INotifyPropertyChanged { public GpsUtil() { } public static GpsUtil CreateInstance() { } public bool IsGpsReady […]

以编程方式将ListViewItem添加到WPF中的Listview

可能重复: WPF ListView – 如何以编程方式添加项目? 如何在C#中完成?

了解WPF Dispatcher.BeginInvoke

我的印象是dispatcher将遵循排队的操作的优先级,并根据优先级或操作添加到队列的顺序(如果相同的优先级)执行操作,直到我被告知这不是WPF UI dispatcher 。 有人告诉我,如果UI线程上的操作持续时间较长,则表示数据库读取UI调度程序,则尝试执行队列中的下一组操作。 我无法接受它,所以决定编写一个示例WPF应用程序,其中包含一个按钮和三个矩形,点击按钮,矩形填充不同的颜色。 并在代码隐藏 private void OnFillColorsClick(object sender, RoutedEventArgs e) { var dispatcher = Application.Current.MainWindow.Dispatcher; dispatcher.BeginInvoke(new Action(() => { //dispatcher.BeginInvoke(new Action(SetBrushOneColor), (DispatcherPriority)4); //dispatcher.BeginInvoke(new Action(SetBrushTwoColor), (DispatcherPriority)5); //dispatcher.BeginInvoke(new Action(SetBrushThreeColor), (DispatcherPriority)6); dispatcher.BeginInvoke(new Action(SetBrushOneColor)); dispatcher.BeginInvoke(new Action(SetBrushTwoColor)); dispatcher.BeginInvoke(new Action(SetBrushThreeColor)); }), (DispatcherPriority)10); } private void SetBrushOneColor() { Thread.Sleep(10 * 1000); Order = “One”; //MessageBox.Show(“One”); BrushOne = Brushes.Red; } […]

我无法将数据绑定到WPF / XAML中的局部变量

我想要一个文本框在我点击它时显示变量的值(1到100的迭代),我不知道我在做什么错了: 当我运行项目时,文本框中不显示任何内容。 在文本框中显示变量的最佳方法是什么? using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace dataBindingTest { /// /// Interaction logic for MainWindow.xaml /// public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } public string myText { […]