Tag: wpf

WPF将控件的图像复制到位图

我正在使用第三方控件,我想从控件创建一个位图(放在剪贴板上或保存到png)。 我已经尝试使用RenderBitmapTarget ,但它只会复制控件,因为它在屏幕上呈现(我的网格比屏幕大)。 我的RenderBitmapTarget代码如下所示: RenderTargetBitmap rtb = new RenderTargetBitmap((int)control.ActualWidth, (int)control.ActualHeight, 96, 96, PixelFormats.Pbgra32); rtb.Render(control); PngBitmapEncoder png = new PngBitmapEncoder(); png.Frames.Add(BitmapFrame.Create(rtb)); MemoryStream stream = new MemoryStream(); png.Save(stream); Image image = Image.FromStream(stream); 我已经尝试指定一个更大的尺寸(在RenderTargetBitmap构造函数中,并为控件指定新的宽度/高度,但两者都在较大的canvas上产生了相同的图像。 有什么想法吗?

如何将ObservableCollection 绑定到WPF中的Checkbox的Listbox

让我用这个问题作为前缀,说明我对C#和WPF都很新。 我正在尝试将一组Boolean值连接到一个包含6个复选框的容器,并在按下按钮时存储这些值的状态。 我假设有一种简单的方法可以做到这一点,因为将复选框绑定到一个集合似乎是一件非常自然的事情,但到目前为止我看到的所有解决方案看起来都过于复杂(例如: http:// merill。 net / 2009/10 / wpf-checked-listbox / )。 我通过修改ListBox的数据模板创建复选框,并将ListBox的ItemsSource设置为ObservableCollection ,但我的问题是我不知道将IsChecked绑定到什么,因为我试图将它绑定到集合中的实际对象,而不是对象的属性。

WPF – 如何以编程方式备份​​/还原LocalDB – ClickOnce

我有一个应用程序使用EF和LocalDB作为它的数据库,由ClickOnce发布。 这是我第一次使用LocalDB,我不知道如何添加一个function到我的应用程序以编程方式备份​​/恢复数据库。 ClickOnce安装的我的应用程序路径: C:\Users\Mahdi Rashidi\AppData\Local\Apps\2.0\NOL11TLW.9XG\CZM702AQ.LPP\basu..tion_939730333fb6fcc8_0001.0002_fd707bbb3c97f8d3 这是数据库文件安装的位置: C:\Users\Mahdi Rashidi\AppData\Local\Apps\2.0\NOL11TLW.9XG\CZM702AQ.LPP\basu…exe_939730333fb6fcc8_0001.0002_none_8c555c3966727e7f 我该如何备份/恢复数据库? 如何通过ClickOnce进一步更新来保护数据库安全? 非常感谢 :)

为什么multithreading访问WPF UI控件的开销?

在开发multithreading应用程序时需要编写大量重复代码时,我是否遗漏了WPF(以及之前的Windows窗体)。 应用程序中的每个UI控件最终都需要额外的代码行来获取和设置每个属性。 internal delegate void SetElementIsEnabledDelegate(UIElement element, bool isEnabled); internal delegate void SetBrushesAndTextDelegate(Brush foregroundBrush, string message); internal delegate void SetCheckBoxCheckedDelegate(CheckBox checkbox, bool checkedValue); internal delegate void SetTextBoxTextDelegate(TextBox richTextBox, string text); internal delegate void SetRichTextBoxTextDelegate(RichTextBox textBox, string text); internal static void SetElementIsEnabled(UIElement element, bool isEnabled) { if (element.Dispatcher.Thread != Thread.CurrentThread) { // Execute the same method, […]

使用CollectionView过滤DataTable时出现错误的原因?

标题为。 所以我成功地使用以下方法将DataTable放入我的CollectionView : ICollectionView _collectionView { get; set; } public ICollectionView collectionView {……} DataTable myDataTable= new DataTable(); myConnection.Open(); dbAdpater.Fill(myDataTable); myConnection.Close(); var collectionList= (newLeadTable as IListSource).GetList(); this.collectionView = CollectionViewSource.GetDefaultView(collectionList); 我知道列表中的每个对象都是DataRowView ,并且我通过迭代数据测试,我可以看到正确存储。 执行代码时, DataGrid正确显示。 但是一旦我添加了Filter: this.collectionView.Filter = new Predicate( (obj) => { return false; //Just for testing propose } ); 当我执行代码时给我错误: 无法创建程序集“TestWPF,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = […]

在运行时将byte 加载到System.Windows.Controls.Image中

我有一个表示.png文件的byte []。 我正在通过WebClient下载此.png。 当WebClient通过URL下载.png I引用时,我得到一个byte []。 我的问题是,如何将一个byte []加载到WPF中的System.Windows.Controls.Image元素中? 谢谢。 注意:这是我在这里提出的问题的补充: 在运行时将一个字节[]加载到一个图像中 。 在我原来的问题中我不够具体,但答案仍然相关。

DependencyProperty作为实例变量?

虽然网络上的大多数代码示例都使用DependencyProperties的静态声明,但在某些情况下,我发现它们被定义为公共只读实例成员。 将DependencyProperty定义为实例成员(而非静态)是否有任何优点? ( 注意:我不想讨论静态成员是好还是坏,更具体地说是DependencyProperty对象的行为)

用户选择子项后,TreeView自动选择父级

在我的窗口中,我有一个TreeView和TextBox。 假设TextBox用于编写自定义脚本,TreeView是一种选择要插入的函数的方法; 想想Crystal Report脚本编辑器。 我的目标是让用户单击TreeView的一个子项,并将该子项插入到TextBox中。 子项是函数签名,位于父节点下。 然后,用户可以导航到TextBox,选择一个function参数并将其替换为另一个function签名。 为此,我处理TreeView的SelectedItemChanged事件,设置TextBox的SelectedText,然后尝试在更改后突出显示文本。 正确交换TextBox的SelectedText。 但是,文本未突出显示,滚动条未滚动到所选文本。 这是我编写的测试项目中的XAML,用于重现行为: 这是代码隐藏: 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 SelectedTextWeirdness { public class Child { public string Name { get; set; } } public class […]

如何在xaml中声明一个故事板并从代码中运行它

我想在点击按钮时增加当前窗口高度。 我用这个代码: private void sendbtn_Click(object sender, RoutedEventArgs e) { DoubleAnimation myDoubleAnimation = new DoubleAnimation(); myDoubleAnimation.From = this.Height; myDoubleAnimation.To = 500; myDoubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(0.5)); Storyboard myStoryboard = new Storyboard(); myStoryboard.Children.Add(myDoubleAnimation); Storyboard.SetTargetName(myDoubleAnimation, this.Name); Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath(Window.HeightProperty)); myStoryboard.Begin(this); } 但我想在xaml中声明我的故事板并从代码中运行它。 但我不知道这是怎么回事?

使用字符串常量更改通知属性

我正在使用一些现有代码,并试图找出在实现INotifyPropertyChanged接口时使用字符串常量作为属性名称的优势(如果有的话)。 例如,这样做: /* * Why use this instead of string literal * in OnPropertyChanged below?? */ public const string CustomerIdPropertyName = “CustomerId”; private int _customerId; public int CustomerId { get { return _customerId; } set { if (_cusomterId != value) { _customerId = value; OnPropertyChanged(CustomerIdPropertyName); } } } 而不是这个: private int _customerId; public int CustomerId […]