|
WPF works for you when your "view" is laid out in a logical manner; yours is not; it is particularly obvious if you look at your XAML in the "document outline" and "design" windows.
StackPanels are hard to control; it's easier / more intuitive using "Grids within Grids".
You should not be altering "heights" and "margins" (from their defaults) to affect positioning; the "styles" (i.e. height, width, etc.) of the individual controls should drive the (automatic) layout.
Start by removing the height and width of the window and look at the properties that relate to its content; e.g. StartupLocation; SizeToContent; WindowState; etc.
|
|
|
|
|
So, I basically took the reordering from Josh Smith:
Drag and Drop Items in a WPF ListView[^]
All I wanted to do now was to draw a line where the new item was inserted, with an arrow. Like the old WinForms style behavior:
Manual reordering of items inside a ListView[^]
The line under was easy to achieve, all I had to was to alter the Style:
<Style x:Key="ItemContStyle" TargetType="ListViewItem">
<Style.Resources>
<LinearGradientBrush x:Key="MouseOverBrush" StartPoint="0.5, 0" EndPoint="0.5, 1">
<GradientStop Color="#22000000" Offset="0" />
<GradientStop Color="#44000000" Offset="0.4" />
<GradientStop Color="#55000000" Offset="0.6" />
<GradientStop Color="#33000000" Offset="0.9" />
<GradientStop Color="#22000000" Offset="1" />
</LinearGradientBrush>
<SolidColorBrush x:Key="SelectedBackgroundBrush" Color="#DDD" />
<SolidColorBrush x:Key="DisabledForegroundBrush" Color="#888" />
</Style.Resources>
<Setter Property="Padding" Value="0,4" />
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<!--
<Setter Property="Border.BorderThickness" Value="0,0,0,0.5" />
<Setter Property="Border.BorderBrush" Value="Transparent"/>
<!--
<Style.Triggers>
<Trigger Property="jas:ListViewItemDragState.IsBeingDragged" Value="True">
<Setter Property="FontWeight" Value="DemiBold" />
</Trigger>
<Trigger Property="jas:ListViewItemDragState.IsUnderDragCursor" Value="True">
<Setter Property="Border.BorderThickness" Value="0,0,0,0.5" />
<Setter Property="Border.BorderBrush" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
SO I looked at the ControlTemplate for the ListViewItems:
ListViewItem ControlTemplate Example[^]
But as soon as I started to add the ControlTemplate Design the correct behavior stopped:
<Style x:Key="ItemContStyle" TargetType="ListViewItem">
<Style.Resources>
<LinearGradientBrush x:Key="MouseOverBrush" StartPoint="0.5, 0" EndPoint="0.5, 1">
<GradientStop Color="#22000000" Offset="0" />
<GradientStop Color="#44000000" Offset="0.4" />
<GradientStop Color="#55000000" Offset="0.6" />
<GradientStop Color="#33000000" Offset="0.9" />
<GradientStop Color="#22000000" Offset="1" />
</LinearGradientBrush>
<SolidColorBrush x:Key="SelectedBackgroundBrush" Color="#DDD" />
<SolidColorBrush x:Key="DisabledForegroundBrush" Color="#888" />
</Style.Resources>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<Grid>
<Polygon
x:Name="poly_PART"
VerticalAlignment="Bottom"
HorizontalAlignment="Left"
Stroke="Red"
Fill="Red"
StrokeThickness="2"
Points="0,1 0,-1 1,0"
Margin="0,0,0,0"
Width="10"
Height="10"
Stretch="Fill"
/>
<ContentPresenter
Content="{TemplateBinding Content}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="Padding" Value="0,4" />
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<!--
<Setter Property="Border.BorderThickness" Value="0,0,0,0.5" />
<Setter Property="Border.BorderBrush" Value="Transparent"/>
<!--
<Style.Triggers>
<Trigger Property="jas:ListViewItemDragState.IsBeingDragged" Value="True">
<Setter Property="FontWeight" Value="DemiBold" />
</Trigger>
<Trigger Property="jas:ListViewItemDragState.IsUnderDragCursor" Value="True">
<!--
<Setter Property="Border.BorderThickness" Value="0,0,0,0.5" />
<Setter Property="Border.BorderBrush" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
How do I fix this issue?
|
|
|
|
|
Here is a common way of having a ViewModel's property with the INotifyPropertyChanged interface:
string text;
public string Text
{
get { return text; }
set
{
if (value != text)
{
text = value;
NotifyPropertyChanged(nameof(Text));
}
}
}
This occupies one line for the member variable and twelve lines for the property.
In my ViewModel I have many properties and the code is getting very huge due to each property need the above body to include the NotifyPropertyChanged method and a member varible.
I prefer my properties to be defined in this way:
public string Text { get; set; }
That property's body is just one line and I don't need to define a member variable.
But I cannot find out how to call the NotifyPropertyChanged method.
Is there a solution to short the body of the property and still have the call to the NotifyPropertyChanged method?
For instance:
* Having a property without the need of a member variable.
* Define the property as close as possible to: public string Text { get; set; } .
* Having the call to the NotifyPropertyChanged in the setter body.
Best regards,
/Steffe
|
|
|
|
|
Mc_Topaz wrote: Is there a solution to short the body of the property and still have the call to the NotifyPropertyChanged method? No.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
|
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
|
|
|
|