Tag: wpf

如何在属性上实现双向绑定?

我知道有很多关于依赖属性的问题,我已经看了很多,但是它们似乎都没有解决我的问题。 我有一个像这样的窗口: 其中MyTextValue只是一个字符串属性,通知何时更改: private string _myTextValue = “Totally different value”; public string MyTextValue { get { return _myTextValue; } set { _myTextValue = value; OnPropertyChanged(); } } public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } TextInputWrapper也很简单: 代码隐藏: public partial class TextInputWrapper : UserControl { public static […]

c#wpf多边形到位图不显示任何内容

对不起,如果您认为这个问题已经得到解答,我确实到处试图找出当我这样做的时候,它没有显示任何内容。 这是我的全部代码: Polygon hexagon = new Polygon(); PointCollection pc = new PointCollection(); double side = 25; double xOffset = 0, yOffset = 0; double r = System.Math.Cos((System.Math.PI / 180) * 30) * side; double h = System.Math.Sin((System.Math.PI / 180) * 30) * side; //Create the 6 points needed to create a hexagon pc.Add(new Point(xOffset, yOffset)); […]

用户编辑WPF DataGrid Cell时如何知道?

我有一个WPF DataGrid 。 用户可以编辑cell的数据。 我想要一个event ,我想检查cell是否为empty 。 用户可以使用Del Backspace Cut选项等清空数据。 给我一个event和event handler来做到这一点。 我已经尝试过OnCellEditEnding event但只有在编辑完成后才会触发。 我希望每次用户inputs时检查cell是否为空。

WPF查找窗口实例

我在类中创建了一个窗口实例,但是我无法直接从其他类访问该窗口的实例。 有没有办法引用我已经使用C#方法创建的窗口实例,可能会搜索打开的应用程序窗口,直到找到我正在尝试访问的仪表板窗口?

棱镜 – 跨区域数据绑定

可以说我有2个区域A和B. 地区A: HAHAHA B区: 这不起作用。 修复此问题的解决方法是什么,因此在区域B中还显示“HAHAHA”?

WPF:如何在XAML中使用您自己的C#代码中的变量来创建样式

WPF和XAML新手在这…. 我需要将XAML代码中的WPF Trigger或DataTrigger到除XAML控件类之外的类中的某些C#代码中。 这非常令人沮丧,因为我读过的所有28,000个教程只给出了涉及已经存在的属性的 Trigger或DataTrigger一个简单示例(例如MouseOver ),它们都没有给出如何将它与您自己的C#代码绑定的示例。 我有一个显示各种报告类型的屏幕。 所有报告类型的XAML都是相同的,但对于诊断报告,我的要求是DataGrid单元格配置为TextBlock.TextAlignment=”Left” ,而所有其他报告(即默认值)应为TextBlock.TextAlignment=”Center” 。 (还有一些其他差异;为了简洁起见,我只是说这是唯一的区别。)我真的不想将整个XAML复制到特殊情况下的诊断报告中,因为其中99%将是与其他报告相同。 要使用Trigger,我想也许我需要我的类inheritanceDependencyObject所以我可以在其中定义DependencyProperty(作为一个WPF新手,我意识到我可能会说一些非常古怪的东西)。 所以在我的C#代码中,我有一个类… namespace MyApplication { public enum SelectedReportType { EquipSummary, EventSummary, UserSummary, DiagSummary } public sealed class ReportSettingsData : DependencyObject { private static ReportSettingsData _instance; // singleton static ReportSettingsData() { new ReportSettingsData(); } private ReportSettingsData() // private because it’s a singleton { if (_instance […]

如何在RichTextBox中的文本上正确应用backgroundcolor

internal string Select(RichTextBox rtb, int index, int length) { TextRange textRange = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd); if (textRange.Text.Length >= (index + length)) { TextPointer start = textRange.Start.GetPositionAtOffset(index, LogicalDirection.Forward); TextPointer end = textRange.Start.GetPositionAtOffset(index + length, LogicalDirection.Backward); rtb.Selection.Select(start, end); rtb.Selection.ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(Colors.LightBlue)); } return rtb.Selection.Text; } 每当调用ApplyPropertyValue来更改所选文本的背景颜色时,它首次运行良好,但在第二次调用时不能正确调整所选文本段的背景颜色。 我怀疑这与调用函数后文件的偏移量有些混乱有关。 解决这个问题的好方法是什么?

NSubstitute vs PRISM EventAggregator:断言调用方法会触发具有正确有效负载的事件

考虑以下方法,通过PRISM EventAggregator更新人员并发布事件,以指示此人已更新。 我想unit testing消息是否与正确的有效负载一起发送。 在这种情况下,这意味着正确的personId。 public void UpdatePerson(int personId) { // Do whatever it takes to update the person // … // Publish a message indicating that the person has been updated _eventAggregator .GetEvent() .Publish(new PersonUpdatedEventArgs { Info = new PersonInfo { Id = personId, UpdatedAt = DateTime.Now }; }); } 我知道我可以创建事件聚合器的替代品: var _eventAggregator = […]

ObservableCollection的反向顺序

我有包含自定义对象的ObservableCollection 。 通常,项目会在列表末尾添加 我在找什么: 从列表开头添加的项目。 这可能只在UI中显示 ,我的整个程序已经在使用此列表中的数据位置。 因此,其中的对象可能不会改变后面代码中的顺序。 此ObservableColection包含Button对象(自定义样式)。 这些按钮显示在ListBox和StackPanel内部,具有水平布局(因此按钮可以很好地放在彼此之后)。 问题: 创建的每个按钮都会收到DateTime 。 新添加的按钮始终具有更新的日期,然后是之前的按钮。 所有计算都发生在计时器内 (目前每秒运行一次)。 所以我基本上在这个时间排序,但是在突然按下3个按钮后,按钮被放置在右手侧(而不是左手侧)。 例如: Btn3:14:15:45(正确) Btn4:14: 16:00( 错误) Btn2:14:15:32(正确)Btn1:14:04:17(正确) 每次都会在列表的开头正确添加前3个按钮。 突然第四个项目被添加到第二个位置。 似乎并不总是比较时间? 每次创建一个按钮时,都会调用CollectionViewSource方法。 CollectionViewSource有问题还是有更好的方法来处理它?

每秒加载新图像

我需要加载每秒(或两个)新图像。 以下代码不起作用: System.Threading.Thread.Sleep(2000); this.Image_LoadImage.Source = new BitmapImage(new Uri(@”D:\\connect2-1.gif”)); System.Threading.Thread.Sleep(2000); this.Image_LoadImage.Source = new BitmapImage(new Uri(@”D:\\connect3-1.gif”)); 我看到的是该应用程序睡眠4秒,然后出现第二个图像。 我该怎么做? 谢谢。