Tag: wpf

WPF自定义控件依赖属性中未知对象的双向绑定问题

我有一个自定义控件 – 为AutoComplete TextBox实现。 我从以下问题得到了所有想法在WPF C#中使用多个控件的组合创建自定义控件 。 在该自定义控件中,他们建议使用以下代码来添加项目, 它的完美工作和双向绑定 。 (this.ItemsSource as IList).Add(this._textBox.Text); 但是,我将以下代码更改为Unknown Object,因此我将IList更改为IList (this.ItemsSource as IList).Add(item); XAML: 但它没有更新ViewModel Property Collection 。 我也在xaml中尝试了以下更改 ItemsSource=”{Binding Collection, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}” 我不知道我在哪里犯了这个错误。 function : CustomControl的TextBox接受来自User的输入并触发ProviderCommand ,该Command根据User Input过滤远程数据并通过AutoItemsSource发送Filtered Collection,此属性被绑定为ListBox内的ItemsSource CustomControl选择Item。 我们可以从ListBox项中选择Item,通过单击Item,它会触发CustomControl类中的Command AddCommand ,它会在CustomControl ItemSource属性中添加所选项。 我在这个Property ItemsSource遇到了双向绑定问题 。 仅从此属性中我们可以将Selected项目作为集合。 这是我的完整源代码 自定义控件C#代码: public class BTextBox : ItemsControl { static BTextBox() { […]

使用taglib在WPF的“图像”框中显示封面图样

我正在制作一名玩家而且我陷入了一个看似简单的问题。 我需要将歌曲的封面艺术显示在一个图像框中。 我找到了这两个解决方案: 这个: var file = TagLib.File.Create(filename); if (file.Tag.Pictures.Length >= 1) { var bin = (byte[])(file.Tag.Pictures[0].Data.Data); PreviewPictureBox.Image = Image.FromStream(new MemoryStream(bin)).GetThumbnailImage(100, 100, null, IntPtr.Zero); } 还有这个: System.Drawing.Image currentImage = null; // In method onclick of the listbox showing all mp3’s TagLib.File f = new TagLib.Mpeg.AudioFile(file); if (f.Tag.Pictures.Length > 0) { TagLib.IPicture pic = f.Tag.Pictures[0]; MemoryStream […]

在Binding中使用StringFormat显示带空格的Hex值

我无法弄清楚如何使用StringFormat显示hex: 08 A4 23 F5 具体来说,我想要每2个字符后面的空格。 我这样做的时候: Text=”{Binding MyIntValue, StringFormat={}{0:x}}” 它看起来像这样: 08A423F5 我的备份计划是在绑定上使用转换器,但我想知道是否可以使用StringFormat来完成它。

如何从列表框中选择项目在WPF中有复选框?

这是ListBox代码: 如何使用C#获取上面ListBox中所有选中的复选框?

Wpf:combobox内的自定义Datagrid

我有一个视图,其中我有一个数据网格,在数据网格中我把一个列作为DataGridTemplateColumn。我把一个combobox放在DataGridTemplate列中,我在combobox内定义了一个数据模板。用户可以在combobox中输入文本结果已过滤进入CustomeDatagrid,用户可以在CustomeDatagrid上选择Item。 这是我的要求: 我尝试实现它。 这是我的XAML: CustomDataGrid: 但是当我点击DatagridTemplate列时,我只得到普通的combobox而不是自定义数据网格。 我的代码有什么问题? 如何在wpf中的combobox内实现上面的(图像)CustomDatagrid? 编辑: 如何通过绑定更改combobox文本时填充customDataGrid?

如何最好地应用WPF MVVM?

我正在尝试设置一个MVVM风格的应用程序,我想我已经通过这些项目的交互并希望有人可以提供帮助。 我在这里做错了吗? 我想我的主要两个问题是 我应该如何从我的模型转向我的观点。 目前我正试图通过转换器来做到这一点。 如果使用转换器是正确的,我该如何正常工作? 我相信Node构造函数上设置的datacontext被包含转换器的XAML取代。 我的课程(简化了一下): IFieldDescription // Interface that is supposed to be the Model public interface IFieldDescription { String Name { get; } bool Disabled { get; } } 节点模型 // Class that is supposed to be the ViewModel public class NodeModel : NotifyPropertyChanged { internal NodeModel() { } public NodeModel(IFieldDescription […]

如何将多边形绑定到WPF中的现有PointCollection?

即使我认为有限的集合有数据(我在调试中检查过),我当前的实现也没有在表单上显示任何内容。 这是一些代码: public event PropertyChangedEventHandler PropertyChanged; PointCollection imagePoints; public PointCollection ImagePoints { get { return this.imagePoints; } set { if (this.imagePoints != value) { this.imagePoints = value; if (this.PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(“ImagePoints”)); } } } } 和相应的xaml: 现在,我通过编写代码完成了所有绑定。 在这个例子中 ,它工作正常,但在我的情况下,点不会出现在多边形上。 有智慧的珍珠吗? 编辑:这是完整的xaml代码 编辑:修改配置文件后,我在输出窗口中找到它: System.Windows.Data Information: 41 : BindingExpression path error: ‘ImagePoints’ property […]

Treeview隐藏 按钮

有谁知道如何隐藏树视图的[ – ]按钮? 树视图永远不会崩溃,因此我不需要根节点具有折叠选项。 我知道我可以使用带有stye的listview来缩进[0]元素,使其像树视图根节点一样。 然而,我正在使用的绑定等类型请求树视图,我不知道如何访问该按钮并禁用它。

绑定工作没有INotifyPropertyChanged,为什么?

这就是我们通常的做法: public class ViewModel : INotifyPropertyChanged { string _test; public string Test { get { return _test; } set { _test = value; OnPropertyChanged(); } } public event PropertyChangedEventHandler PropertyChanged; public void OnPropertyChanged([CallerMemberName] string property = “”) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property)); } 现在我们的属性可以被视图中的多个元素使用,例如: 更改TextBox值将更新TextBlock内容。 我们可以在视图模型中设置值,视图会自动更新。 如果我们写这样的视图模型 public class ViewModel { public string Test { get; […]

在WindowsFormsHost上使用RenderTargetBitmap

我在WindowsFormsHost有一组控件,我想捕获当前视图并将其保存为图像,但是我只能在图像中看到一些Panel 。 是否可以将WindowsFormsHost用作“Visual”并捕获包装的控件? 看我的例子: 如果我basePanel添加一个Button或其他内容,那么在使用以下代码导出到PNG时,这将不可见: RenderTargetBitmap rtb = new RenderTargetBitmap(basePanel.Width, basePanel.Height, 96, 96, PixelFormats.Pbgra32); rtb.Render(windowHost); PngBitmapEncoder pnge = new PngBitmapEncoder(); pnge.Frames.Add(BitmapFrame.Create(rtb)); Stream stream = File.Create(“test.jpg”); pnge.Save(stream); stream.Close(); 关于为什么这可能不起作用的建议,也许是可能的解决办法? 我想这不是真的想用这种方式工作,但人们真的希望!