Tag: wpf

如何使用PropertyChangedCallBack

我有一个绑定到依赖属性的TextBox,我已经实现了一个PropertyChangedCallBack函数,当文本更改时我需要调用textbox.ScrollToEnd()但我不能因为PropertChanged函数需要是静态的,有没有办法绕过这个? static FrameworkPropertyMetadata propertyMetaData = new FrameworkPropertyMetadata(“MyWindow”, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, new PropertyChangedCallback(TextProperty_PropertyChanged)); public static readonly DependencyProperty TextProperty = DependencyProperty.Register(“TextProperty”, typeof(string), typeof(OutputPanel), propertyMetaData); private void TextProperty_PropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) { textbox.ScrollToEnd(); //An object reference is required for the non-static field. } public string Text { get { return this.GetValue(TextProperty) as string; } set { this.SetValue(TextProperty, value); //textbox.ScrollToEnd(); // […]

以编程方式在WPF中创建网格作为模板

我想以编程方式创建一个带有样式的基本用户控件。 在这种风格中我想添加一个Grid (没问题),但是我不能在这个网格中添加列定义。 我的示例代码是 ControlTemplate templ = new ControlTemplate(); FrameworkElementFactory mainPanel = new FrameworkElementFactory(typeof(DockPanel)); mainPanel.SetValue(DockPanel.LastChildFillProperty, true); FrameworkElementFactory headerPanel = new FrameworkElementFactory(typeof(StackPanel)); headerPanel.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal); headerPanel.SetValue(DockPanel.DockProperty, Dock.Top); mainPanel.AppendChild(headerPanel); FrameworkElementFactory headerImg = new FrameworkElementFactory(typeof(Image)); headerImg.SetValue(Image.MarginProperty, new Thickness(5)); headerImg.SetValue(Image.HeightProperty, 32d); headerImg.SetBinding(Image.SourceProperty, new Binding(“ElementImage”) { RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent) }); headerPanel.AppendChild(headerImg); FrameworkElementFactory headerTitle = new FrameworkElementFactory(typeof(TextBlock)); headerTitle.SetValue(TextBlock.FontSizeProperty, 16d); headerTitle.SetValue(TextBlock.VerticalAlignmentProperty, VerticalAlignment.Center); […]

WPF:获取控件绑定在后面的代码的属性

我试图找到一种方法来获取控件绑定的属性(在c#中)。 如果我有以下内容: 我现在正试图获取SelectedItem所绑定的位置,即结果应为“Name”。 然后在代码中我需要用ViewModel属性做一些事情。 问题是我不能只对它进行硬编码,因为它是一种需要使用表单上每个控件的通用方法。 谢谢,理查德

WPF密钥是数字或数字

我在我的窗口中有previewKeyDown方法,我想知道按下的键只有AZ字母或1-0数字(没有anyF1..12,输入,ctrl,alt等 – 只是字母或数字)。 我已经尝试过Char.IsLetter ,但是我需要给char,所以e.key.ToString()[0]不起作用,因为它几乎每次都是一个字母。

指定的元素已经是另一个元素的逻辑子元素。 首先断开它

这是我想要将FrameworkElement附加到新窗口以将其发布到PNG文件时的错误。 所以我的想法是删除父子链接,调用我的方法,并使用以下代码再次添加子代: this.RemoveLogicalChild(element); PublishFrameworkElement(element, stream); this.AddLogicalChild(element); 但我得到了完全相同的错误…… 我在这里看了很多关于这个错误的问题,但没有回答我的问题我错过了什么? 编辑:这是适合我的代码: var element = _GeneratedContent as FrameworkElement; var ParentPanelCollection = (element.Parent as Panel).Children as UIElementCollection; ParentPanelCollection.Clear(); FileStream stream = […] if (element != null) { PublishFrameworkElement(element, stream); ParentPanelCollection.Add(element); } stream.Close();

如何从CollectionViewSource获取项目数?

我正在使用CollectionViewSource来过滤ListBox中显示的记录。 xaml如下。 这是我的代码behing(请不要介意这个代码隐藏,在实际的应用程序中,我使用最好的MVVM来实现这个场景)。 public partial class ListBoxFilterUsingCollectionViewSource : Window { private string _text=””; private readonly CollectionViewSource _viewSource; public ListBoxFilterUsingCollectionViewSource() { InitializeComponent(); _viewSource = this.FindResource(“cvs”) as CollectionViewSource; } private void CollectionViewSource_Filter(object sender, FilterEventArgs e) { var character = e.Item as Character; e.Accepted = character != null && character.First.ToLower().Contains(_text.ToLower()); } private void txtSearch_TextChanged(object sender, TextChangedEventArgs e) { […]

如何在后台线程中创建WPF控件?

我有创建后台线程的方法来做一些动作。 在这个后台线程中我创建了对象。 但是在运行时创建时这个对象给了我一个例外: 调用线程必须是STA,因为许多UI组件都需要这个。 我知道我必须使用Dispatcher来反映UI的内容。 但在这种情况下,我只是创建一个对象,而不是用UI迭代。 这是我的代码: public void SomeMethod() { BackgroundWorker worker = new BackgroundWorker(); worker.DoWork += new DoWorkEventHandler(Background_Method); worker.RunWorkerAsync(); } void Background_Method(object sender, DoWorkEventArgs e) { TreeView tv = new TreeView(); } 如何在后台线程中创建对象? 我使用WPF应用程序

BackgroundWorker读取数据库和更新GUI

当我查询数据库并在BackgroundWorker中的DoWork中更新DataGrid ,我试图保持我的不确定ProgressBar动画顺利运行。 我知道我无法在DoWork更新UIThread,所以我使用Dispatcher.BeginInvoke来访问DataGrid ,但我仍然收到了由另一个线程拥有的exception。 问题1 为什么会这样? DoWork应该在UIThread拥有的BackgroundWorker的线程上,Dispatcher应该允许我的DoWork访问我的GUI。 进一步研究BackgroundWorker建议应该在ProgressChanged()处理UI操作。 但是,如果我将所有数据库/ GUI操作移动到ProgressChanged()并在DoWork() Thread.Sleep(5000)上放置一个Thread.Sleep(5000)来模拟某些东西,以便为ProgressChanged()提供足够的时间来运行,那么GUI就不会被更新,虽然ProgressBar继续其不确定的平滑动画。 问题2 我在BackgroundWorker线程上调用了Thread.Sleep(5000)。 ProgressChanged()应该花5秒时间查询数据库并更新GUI。 为什么不? XAML: Search C# private BackgroundWorker bgw = new BackgroundWorker(); private void btnSearch_Click(object sendoer, RoutedEventArgs e) { bgw.WorkerReportsProgress = true; bgw.ProgressChanged += ProgressChanged; bgw.DoWork += DoWork; bgw.RunWorkerCompleted += BGW_RunWorkerCompleted; bgw.RunWorkerAsync(); } private void DoWork(object sender, DoWorkEventArgs e) { // Thread.Sleep(5000); using […]

WPF数据绑定标签内容

我正在尝试使用数据绑定创建一个简单的WPF应用程序。 代码看起来很好,但是当我更新我的属性时,我的视图没有更新。 这是我的XAML: 这是我的代码隐藏: namespace Calculator { public partial class MainWindow { public MainWindow() { DataContext = new CalculatorViewModel(); InitializeComponent(); } } } 这是我的视图模型 namespace Calculator { public class CalculatorViewModel : INotifyPropertyChanged { private String _calculatorOutput; private String CalculatorOutput { set { _calculatorOutput = value; NotifyPropertyChanged(); } } public event PropertyChangedEventHandler PropertyChanged; protected virtual void […]

如何在WPF中处理TabItem单击事件?

在我的应用程序中,我使用了WPF TabControl我想处理TabItem click事件。 我如何实现它?