Tag: wpf

如何在AutoGeneratingColumn事件期间根据其值设置数据网格单元格的背景?

我仍然在操纵细胞背景,所以我问了一个新问题。 用户“HB”写道我可以在AutoGeneratingColumn事件期间实际设置单元格样式 – 根据值更改DataGrid单元格颜色 。 问题是我不知道该怎么做。 我想要的:根据每个单元格的值设置不同的背景颜色 。 如果值为null我也希望它不可 点击 (我猜可以集中精力)。 我有/我想做什么: private void mydatagrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e) { foreach (Cell cell in e.Column) { if (cell.Value < 1) { cell.Background = Color.Black; cell.isFocusable = false; } else { cell.Background = Color.Pink; } } } 这只是伪代码。 在列自动生成期间是否可以这样做,如果是这样,我如何编辑我的代码以使其有效? 我读到了有关值转换器的信息,但我想知道它是否可以通过编程方式实现,而无需编写XAML。 请理解我还是C#/ WPF / DataGrid的初学者。 解决方案part1: 我用了我接受的答案。 把它放入 […]

MVVM light Messenger中的操作,局部变量和垃圾收集的奇怪行为

我对MVVM Light中的Messenger系统有一个非常奇怪的问题。 这很难解释,所以这里有一个小程序来演示这个问题: using System; using GalaSoft.MvvmLight.Messaging; namespace TestApp { class Program { static void Main(string[] args) { var prog = new Program(); var recipient = new object(); prog.RegisterMessageA(recipient); prog.RegisterMessageB(recipient); prog.SendMessage(“First Message”); GC.Collect(); prog.SendMessage(“Second Message”); } public void RegisterMessageA(object target) { Messenger.Default.Register(this, (Message msg) => { Console.WriteLine(msg.Name + ” recieved by A”); var x = […]

如何将MahApps.Metro捆绑到单个exe中

由于MahApps.Metro,我很难将使用SmartAssembly 6(评估/试用版)将我的C#WPF项目中的所有依赖项捆绑到单个exe中。 这个结论是在创建一个除了MahApps.Metro之外什么都没有的完全空项目时得出的,但仍然无法捆绑它。 它会引发内部exception的exceptionSystem.IO.FileNotFoundException: Could not load file or assembly ‘System.Windows.Interactivity, Version=4.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35’ or one of its dependencies. The system cannot find the file specified. System.IO.FileNotFoundException: Could not load file or assembly ‘System.Windows.Interactivity, Version=4.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35’ or one of its dependencies. The system cannot find the file specified. 我花了一天半的时间试图解决这个问题,谷歌搜索错误,尝试我能找到的每一个建议,然后发布官方MahApps.Metro聊天( https://gitter.im/MahApps/MahApps.Metro )。 我尝试了各种各样的变化,我删除了System.Windows.Interactivity dll,添加它,将它移动到另一个路径等。 使用NuGet和.NET […]

WPF应用程序使用新的csproj格式

我正在尝试将使用旧csproj格式定义的WPF项目迁移到VS 2017下的新格式。 使用我在如何将Wpf项目迁移到新的VS2017格式时发现的信息,我能够获得成功构建的大部分内容 。 但是我坚持要克服这个错误: 错误CS5001:程序不包含适用于入口点的静态“主”方法 我的新式csproj文件如下: $(MSBuildExtensionsPath)\$(VisualStudioVersion)\Bin\Microsoft.CSharp.targets winexe net47 winexe 如何配置csproj文件以便构建入口点? 更新 根据关于A​​pplicationDefinition的提示,我能够让项目进行编译。 我无法在BuildAction中设置ApplicationDefinition – 它不是其中一个选择 – 但必须手动编辑csproj文件以包含它。 这是工作版本: $(MSBuildExtensionsPath)\$(VisualStudioVersion)\Bin\Microsoft.CSharp.targets winexe net47 winexe MsBuild:Compile Designer 另请注意指令上的Excludefilter。 有必要让MSBuild不要尝试两次编译App.xaml.cs。

内存未在WPF映像中发布

我在Canvas加载和卸载图像。 我使用下面的代码加载Image 。 在加载我的Image之前,内存消耗为14.8MB。 Canvas c = new Canvas(); Image im = new Image(); ImageSource src = new BitmapImage(new Uri(@”E:Capture.png”)); im.Source = src; im.Height = 800; im.Width = 800; c.Children.Add(im); homegrid.Children.Add(c); //homegrid is my grid’s name Image显示正确,内存消耗现在为20.8MB。 然后我通过以下代码卸载了Image : foreach (UIElement element in homegrid.Children) { if (element is Canvas) { Canvas page = element as […]

绑定到列表会导致内存泄漏

当我将ListBox的ItemsSource绑定到List时,绑定引擎在控件消失后保持列表元素。 这会导致所有列表元素保留在内存中。 使用ObservalbleCollection时问题就消失了。 为什么会这样? 窗口标记内的xaml GC 代码背后: public MainWindow() { InitializeComponent(); DataContext = new ViewModel(); } private void Button_Click(object sender, RoutedEventArgs e) { this.DataContext = null; ContentControl.Content = null; GC.Collect(); GC.WaitForPendingFinalizers(); } 视图模型 class ViewModel : INotifyPropertyChanged { //Implementation of INotifyPropertyChanged … //Introducing ObservableCollection as type resolves the problem private IEnumerable _list = new List […]

即使应用程序没有聚焦,钩子/检测窗口语言也会改变

有没有办法检测即使我的应用程序不在焦点时Windows / OS语言是否发生了变化? 到目前为止,只有当应用程序集中使用时,我才能实现我想要的: string language = “”; System.Windows.Input.InputLanguageManager.Current.InputLanguageChanged += new System.Windows.Input.InputLanguageEventHandler((sender, e) => { language = e.NewLanguage.DisplayName; MessageBox.Show(language); }); 但是你可以理解,这不是我想要的…… 我正在考虑其他解决方案,例如挂钩改变语言的键(例如alt + shift)但我无法知道当前使用的语言和用户可以更改默认热键… 非常感谢您的帮助。

异步添加到ObservableCollection(或替代)

这就是我所拥有的 – 一个ListBox,其ItemsSource设置为ObservableCollection – 其中T是我的自定义类,表示一个文件,只包含2个DependencyProperties:Filename和ThumbnailPath。 – 列表框还定义了自定义DataTemplate,以便在其下很好地显示图像和文件名。 列表框的目的是显示当前文件夹中的video文件(在TreeView中选择),缩略图(异步生成;不是此问题的一部分)。 因此,当我更改TreeView中的文件夹时,ObservableCollection将被清除并再次填充,这将自动反映在ListBox项目中。 问题在于:UI变得无法响应,更新需要几秒钟。 同样,缩略图在这里没有意义(我尝试禁用它们)。 我认为花费最多时间的是构建我自定义类的50-100个实例及其可视化表示 – 它必须为每个实例初始化一个Image对象。 但这只是我的猜测 – 你能否确认或排除这种可能性? 我开始认为ObservableCollection可能不是这里的方式 ,因为根据我的阅读和我尝试的内容,没有办法异步添加项目,至少如果这些项目是DependencyObjects。 我尝试使用BackgroundWorker创建我的类实例,并将它们添加到ProgressChanged事件处理程序中的集合中,但它会抛出exception(某些线程与dependencyobjects问题)。 有什么东西我不见了吗? 或者通过简单地抛弃ObservableCollection并编写一个好的旧异步for循环来添加项目,我会更好吗?

prism:ViewModelLocator.AutoWireViewModel =“True”对于未引用的程序集不起作用

我在引导程序中加载我的模块: protected override IModuleCatalog CreateModuleCatalog() { var moduleCatalog = new DirectoryModuleCatalog(); moduleCatalog.ModulePath = @”.\Modules”; return moduleCatalog; } 在shell项目中引用模块的项目时,它可以正常工作。 正确的ViewModels将由以下附加属性注入。 <UserControl prism:ViewModelLocator.AutoWireViewModel="True" [..] 除非删除项目引用,否则棱镜将不再设置ViewModel:ViewModelLocator.AutoWireViewModel =“True”。 有谁知道是什么原因引起的? 您可以在项目https://github.com/mfe-/Get.the.solution.Prism.Demo上观察此行为。 我怎样才能解决这个问题?

wpfcombobox结合

嗨,我正在尝试将List 绑定到组合框。 public OfferEditPage() { InitializeComponent(); cusmo = new CustomerViewModel(); DataContext = this; Cusco = cusmo.Customer.ToList(); customer.ItemsSource = Cusco; customer.DisplayMemberPath = “name”; customer.SelectedValuePath = “customerID”; customer.SelectedValue = “1”; } 我变得没有错误,但Combobox总是空的。 库斯科是我名单的财产。 我不知道这段代码有什么问题。 你能帮助我吗? 电贺 public class Customer { public int customerID { get; set; } public string name { get; set; } public string surname […]