|
|
No.
However you can make your declaration easier by using Code Snippets[^].
Here is my MVVM property snippet
="1.0"="utf-8"
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
<Title>SnippetFile1</Title>
<Author>Lloyd</Author>
<Description>INotifyPropertChanged property</Description>
<HelpUrl>
</HelpUrl>
<Shortcut>propn</Shortcut>
</Header>
<Snippet>
<Declarations>
<Object Editable="true">
<ID>Type</ID>
<ToolTip>Type</ToolTip>
<Default>object</Default>
<Function>
</Function>
<Type>
</Type>
</Object>
<Literal Editable="true">
<ID>Property</ID>
<ToolTip>Property</ToolTip>
<Default>Property</Default>
<Function>
</Function>
</Literal>
</Declarations>
<Code Language="csharp"><![CDATA[</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
|
|
|
|
|
There is a way to do this, but it requires an extra bit of build infrastructure. You can, relatively easily, use an AOP (aspect-oriented programming) solution like PostSharp[^] or you could use a Roslyn analyzer/generator[^].
This space for rent
|
|
|
|
|
The "notify patterns" used in this case are "standard"; but not necessarily THE ONLY way.
If performance is not any issue, and UI updates are localized (usually), then simply issue a "NotifyPropertyChanged(string.Empty)" and the "UI engine" will "refresh" ALL output fields.
Used judiciously, it can make more sense to do "notifications" from your main logic than within a "setter"; particularly when it comes to real-time updates that reference multiple "outputs" for computing other outputs.
As an alternative, one can "queue" names of properties that were changed, remove duplicates, then pump them through a notify call; if that fits.
You can then leave the properties as automatic gets and setters.
Benchmark.
|
|
|
|
|
I usually do this (took the code from somwhere but I cant remember the referance):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;
public abstract class NotifierBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
{
if (Equals(storage, value))
{
return false;
}
storage = value;
this.OnPropertyChanged(propertyName);
return true;
}
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
Then I simply call it like this:
private string m_Name ="";
public string Name
{
get { return m_Name; }
set
{
SetProperty(ref m_Name, value);
}
}
I just have a custom snippet to deal with the proeprty coding.
|
|
|
|
|
I applaud your construction but to save 1 if statement and 1 line of code. You ROI is minimal
I use a snippet for the entire property structure and a region surrounding all the properties. Saving half a dozen lines of code per property is just not that high on my todo list.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
I have a wonderful WPF grid that I downloaded from a Codeproject article below. The problem is the grid doesn't do column sorting. Has anyone modified this grid WPF grid for column sorting? Article: DataView Paging in WPF[^]
|
|
|
|
|
Even though this article is 6 years old, the best place to ask this would be in the comments at the end of the article. That way the author would be notified of your question, and any additional discussion that follows would remain associated with the article for others to find in the future.
"Fairy tales do not tell children the dragons exist. Children already know that dragons exist. Fairy tales tell children the dragons can be killed."
- G.K. Chesterton
|
|
|
|
|
You are correct. The comments after article do expand on the article itself. Unfortunately nothing about column sorting. I really want to use this for my project but it requires sorting. Any ideas?
|
|
|
|
|
|
[REDACTED]
Found this[^]
If it's not broken, fix it until it is
modified 30-Jun-16 15:02pm.
|
|
|
|
|
I am starting a new WPF project that's framework is going to be some sort of a tabbed view dashboard. In ASP I would use a master page with a tab control and a content page for each view. I am new to XAML and was hoping that someone knew of a basic article with example code to get me started. If anyone can point me in the right direction that would be great.
-- modified 28-Jun-16 18:47pm.
|
|
|
|
|
You're going to have to read this[^] a few times but it should give you a fair idea about how to pull together an application that suits your needs.
This space for rent
|
|
|
|
|
Sacha Barber[^] has written a load of excellent articles on WPF, from beginner to advanced framework.
|
|
|
|
|
In WPF:
There is a "main window" that would correspond to your "master page".
You can add a WPF "Tab Control" (to the window) that can contain multiple "Tab Item" controls.
The "content" for each tab can be added directly (e.g. as Text Blocks (labels), Text Boxes, Buttons, etc.); however, one generally creates "User Controls" to package UI elements into a view / content page which would then be added to a tab (as a child).
WPF uses "styles" in a way that is similar to CSS in order to propagate styling throughout the visual tree (of UI elements).
|
|
|
|
|
The article below makes doing WPF views just like MVC:
<a href="http://www.codeproject.com/Tips/678020/Master-page-in-WPF">Master page in WPF?</a>[<a href="http://www.codeproject.com/Tips/678020/Master-page-in-WPF" target="_blank" title="New Window">^</a>]
|
|
|
|
|
I have a models project with a base model. In the base model I have:
public class ModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(string propertName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertName));
}
}
} The model has:
public class MonthViewDayModel : ModelBase
{
private bool _IsVisible;
public bool IsVisible
{
get { return _IsVisible; }
set
{
if (_IsVisible != value)
{
_IsVisible = value;
RaisePropertyChanged("IsVisible");
}
}
}
} When I step into the base the PropertyChanged event is ALWAYS null, yet I'm bound to these models in the XAML. The DataContext is being set.
What's wrong here?
If it's not broken, fix it until it is
modified 29-Jun-16 3:53am.
|
|
|
|
|
This is my base model handler - you seem to missing the assignment, caveat I have not looked at this in years!
public void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = this.PropertyChanged;
<pre>
if (handler != null)
{
if (propertyName != "IsDataChanged")
{ IsDataChanged = true; }
handler(this, new PropertyChangedEventArgs(propertyName));
}
}</pre>
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
That looks OK (though I would prefer to copy the eventhandler and test that copy for null, but that should make a difference in rare cases only).
Where did you set the break point to step into that code? Was that action initiated by some change in the GUI?
|
|
|
|
|
Could you post your XAML that goes along with your view model code? From what it looks like here its all set up right on your code end.
|
|
|
|
|
Anyone know where I can find something like[^] this except for everything in WPF?
If it's not broken, fix it until it is
|
|
|
|
|
|
We have a MindFusion.RealTimeCharting control that has been extensively modified by us, so talking to them probably won't help.
On a tab control I have User Control, and in that is the WinForms chart control. We want to create a report from the UI. A "Report" is an image that's created and opened in Windows Photo Viewer.
For the purposes of reporting I've written a class called PrintLib that takes the element you want to print, creates an image of it, and opens the Windows Photo Viewer with the image displayed.
The problem is that the actual contents of the chart, a Sine Wave, doesn't show up. To correct this we added a function to the chart control called 'GetPrintBitmap' which returns a DrawingVisual. We then have to call this in code to produce an image file which is the used to replace the image of the XAML element at runtime.
What we would like is to have this happen when the element is rendered. Basically, we need to force WPF to draw the Sine Wave from the WinForms control when the XAML element image is created.
What I have now does work, except that I have to call a function in the WinForms control to get the bitmap back. Here's what I do have:
The process starts here. Element is the outer control containing the User Control, which contains the WinForms control:
public void Print(ItemsControl element)
{
ChartViewModel vm = ChartCollection[0];
RealTimeChart chart = vm.GetChart();
var dv = chart.GetPrintBitmap();
PrintLib.GenerateReport(element, dv);
}
Here is the significant portion of the GenerateReport method:
public static void GenerateReport(FrameworkElement element, DrawingVisual dv)
{
App.Current.Dispatcher.Invoke(() =>
{
var width = (int)element.ActualWidth;
var mainImageName = string.Empty;
if (dv == null)
{
mainImageName = CreateImageFromView(element, width);
}
else
{
mainImageName = DrawImage(dv, width, (int)element.ActualHeight);
}
.
.
.
});
}
private static string CreateImageFromView(FrameworkElement element, int width)
{
var rect = VisualTreeHelper.GetDescendantBounds(element);
var dv = new DrawingVisual();
using (var ctx = dv.RenderOpen())
{
var brush = new VisualBrush(element);
ctx.DrawRectangle(brush, null, new Rect(rect.Size));
}
var height = (int)element.ActualHeight;
if (height == 0)
{
height = (int)element.Height;
}
if (height == 0)
{
var message = "UI Element height is 0 and is not valid";
Debug.Print(message);
throw new ArgumentException(message);
}
return DrawImage(dv, width, height);
}
private static string DrawImage(DrawingVisual dv, int width, int height)
{
var rtb = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32);
rtb.Render(dv);
var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(rtb));
var fileName = GetUniqueFileName("png");
using (var fs = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None))
{
encoder.Save(fs);
}
return fileName;
}
So essentiially we don't want to have create the DrawingVisual from the WinForms control in code. Is it possible to get WPF to render the WinForms control's contents when it renders the rest of it?
Thanks
If it's not broken, fix it until it is
|
|
|
|
|
Based on your description, and WYSIWYG, I would just capture the Window's client area as an image.
Lots of C# code around to do that.
|
|
|
|
|
That's what my code already does, the problem is that the WinForms portion doesn't render as part of the WPF rendering process, so I'm calling a method on the WinForms control to return it's visible area as a bitmap, then replacing it ove the Winforms control.
I was hopeing there weas some way of forcing the WinForms control to render in the WPF rendering when the entire view is rendered.
If it's not broken, fix it until it is
|
|
|
|