|
It's 3 lines of XAML too using the Blend SDK and event to command.
I disagree about what you claim a VM is for. It's to package data and support the view. GUI properties are allowed as are business rules. If I have a complex UI where its starting to be too many properties, it needs to be refactored. Into a user control or multiple user controls perhaps.
Controlling the focus is not something I've had to implement from the VM / MVVM world, so I can't say off the top of my head how I'd approach it, but I certainly wouldn't use code behind. I'd probably make it a UserControl.
FYI, code behind is allowed in user controls, it's not a good move for a view. For one thing, you split the code for the view into 2 places. Not good.
|
|
|
|
|
I use the Blend SDK interactivity + a pretty standard event to command mapper. Ends up being 1 line added to the XAML namespaces, 2 or 3 lines added to each control I want to trap events for + 1 line for each event. No big deal. Pretty standard way to handle events in MVVM... convert 'em to a command. Thats the POH & Sascha way anyways .
|
|
|
|
|
SledgeHammer01 wrote: The VM knows it needs to show a dialog or window or slide something into view via an animation, or whatever.
That's where I differ. The VM (in my world) only knows that another VM needs to execute - everything works (in principal) even if there is no view - the VM has no right to know that a view is to be shown modally, slid in or animated - that's the View's responsibility.
I keep ALL GUI stuff out of the Views
SledgeHammer01 wrote: Doing it the way you suggested means VM A knows about VM B and intimately knows VM B wants View B attached to it, the DataContext set, etc.
Actually I use a controller to communicate between View Models - so generally one VM doesn't know about another.
e.g.
User clicks button on View1 to edit selected customer.
Command "Edit Custeomr" on VM1 executes - it knows which is the selected customer by binding.
VM1 sends message "Customer Selected For Edit"
Controller is subscribed to message - receives it, sources the relevant VM (the 'edit customer' VM) (and possibly checks to see if there already is one editing that customer) and instantiates it (and its associated View.
SledgeHammer01 wrote: Going the way I suggested is more consistent with how the web works. I don't do web stuff, so its kind of ironic... but on the web, you redirect to the .html (the "view"), not the .cs (the "view model").
The way the web works isn't necessarily a 'good' way, though
And on the web, the View is the HTML and the VM is more accurately Javascript - the .cs the controller/model
It is interesting that there are so many different ways to implement similar patterns.
MVVM # - I did it My Way
___________________________________________
Man, you're a god. - walterhevedeich 26/05/2011
.\\axxx
(That's an 'M')
|
|
|
|
|
You can write custom DataTemplateSelector and then use it in e.g. ContentControl as a ContentTemplateSelector.
For example the following selector looks for desired datatemplate in control's resources by MaterialType:
public class MaterialDataTemplateSelector : DataTemplateSelector
{
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
FrameworkElement element = container as FrameworkElement;
MaterialsViewModel materials = item as MaterialsViewModel;
string resKey = "materialsTemplateFor" + Enum.GetName(typeof(MaterialType), materials.MaterialType);
return element.FindResource(resKey) as DataTemplate;
}
}
Here are the resources you need in your MaterialsView control:
<DataTemplate x:Key="materialsTemplateForType1">
<views:MaterialType1View />
</DataTemplate>
<DataTemplate x:Key="materialsTemplateForType2">
<views:MaterialType2View />
</DataTemplate>
<views:MaterialDataTemplateSelector x:Key="materialDataTemplateSelector" />
And this is how you use it in your control:
<ContentControl ContentTemplateSelector="{StaticResource materialDataTemplateSelector}" />
Hope it helps.
Gabriel Szabo
|
|
|
|
|
Your solution is technically "correct", however, it is doing it the hard way. For a full blown app, you'd have to write hundreds of these template selectors. Too much work .
|
|
|
|
|
The template selector can be made more general. Data templates and material type could be e.g. injected from the hosting xaml. So you don't really need write that many selectors .
I see the selector and your loader template approach philosophically equivalent. However there is a difference from the testing point of view. A unit test for selector is pretty straightforward. But how would you test the data triggers in loader template?
Gabriel Szabo
|
|
|
|
|
Yeah, same thing technically like I said. You'd have to write a template selector for every data type / decision. If you are just deciding whether to run the PC or tablet view, then yeah, you'd just need one selector. You'd have to copy & paste more XAML around though .
Let's be honest about unit tests in the real world... nice concept, but very few companies in this day and age want to invest in that. Often times lots of unit tests are a huge hinderance. I worked at a place that did invest in lots of unit tests and lots of bugs got through because there was no / little manual testing and the mgr believed he could get 100% coverage by getting you to do lots of unit tests. Lots of companies are realizing that they can hire 50 QA guys in india to click buttons vs. hiring one software dev onshore to write unit tests and end up with better quality software the manual way cuz there are more eyes on the product and the devs spend more time writing / testing new features vs. writing unit tests.
Does your place do automated testing? Is all your code covered by unit tests? Do you have mock services in place to test all your dialog return code paths? .
|
|
|
|
|
Hello,
I'm new to WPF and I'm trying to make at run time a binding to an element that is the value in a dictionary corresponding to a certain key.
I made several tries but the best result I have obtained is to have a binding that is triggered only one time. This is the code.
DictThresholds[thresholdKey] = thresholdValue;
HorizontalLine newThresholdLine = new HorizontalLine();
newThresholdLine.StrokeThickness = 1;
newThresholdLine.Stroke = Brushes.Red;
var binding = new Binding();
binding.Source = DictThresholds[thresholdKey];
newThresholdLine.SetBinding(HorizontalLine.ValueProperty, binding);
plotter.Children.Add(newThresholdLine);
class ThresholdDictionary : INotifyCollectionChanged
{
#region Events
public event NotifyCollectionChangedEventHandler CollectionChanged;
#endregion Events
#region Properties and Attributes
private Dictionary<string, Double> _dictThreshold;
public Double this[string key]
{
get { return _dictThreshold[key]; }
set
{
object oldValue = null;
if (_dictThreshold.ContainsKey(key))
{
oldValue = _dictThreshold[key];
}
_dictThreshold[key] = value;
NotifyPropertyChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, value, oldValue));
}
}
#endregion Properties and Attributes
#region Constructors
public ThresholdDictionary()
{
_dictThreshold = new Dictionary<string, Double>();
}
#endregion Constructors
private void NotifyPropertyChanged(NotifyCollectionChangedEventArgs e)
{
if (CollectionChanged != null)
{
CollectionChanged(this, e);
}
}
}
}
I would like that the HorizontalLine to be updated each time I update the value corresponding to that key.
Hope you can help me!
Regaards,
Alessandro
|
|
|
|
|
Why not try setting the Binding.Source to the DictThresholds and have a simple converter that does the dictionary lookup using the thresholdKey as the Binding.ConverterParameter ?
var binding = new Binding();
binding.Source = DictThresholds;
binding.Converter = new DictionaryLookupConverter();
binding.ConverterParameter = thresholdKey;
newThresholdLine.SetBinding(HorizontalLine.ValueProperty, binding);
class DictionaryLookupConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
object result;
IDictionary dict = value as IDictionary;
if (dict == null || parameter == null || !dict.TryGetValue(parameter, out result))
{
return DependencyProperty.UnsetValue;
}
return result;
}
}
This works for anything that implements IDictionary . I leave it as an exercise to extend it for IDictionary<TKey, TValue> .
|
|
|
|
|
You're doing it wrong if you are binding from code. Your example can easily be done via XAML. Have an ItemsControl and style it with a DataTemplate.
|
|
|
|
|
Have a look at the ObservableCollection and see how you can implement it in your code.
|
|
|
|
|
Hello,
I have something strange, i have defined two Event Triggers with atched commands in my button:
<i:EventTrigger EventName="MouseLeftButtonDown">
<i:InvokeCommandAction Command="{Binding StartCommand}"/>
</i:EventTrigger>
<i:EventTrigger EventName="MouseLeftButtonUp">
<i:InvokeCommandAction Command="{Binding StopCommand}"/>
</i:EventTrigger>
LEFT MOUSE BUTTON SCENARIO:
When i use those Events:MouseLeftButtonDown ,MouseLeftButtonUp
the StartCommand invoked when i press(with left mouse button) down the button but when i relese the mouse the stop command is not invoked.
RIGHT MOUSE BUTTON SCENARIO:
When i use those Events:MouseRightButtonDown ,MouseRightButtonUp
the StartCommand invoked when i press(with right mouse button) down the button and the StopCommand invoked when i release the button as expected.
What is the problem why the StopCommand is not invoked?
How can i fire off a command when a button is pressed(with left mouse button) and fire off another command when the button released?
Thanks
|
|
|
|
|
Have you tried using PreviewMouseLeftButtonUp ?
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
can you post the code for Mouse down
|
|
|
|
|
What WPF Frameworks are available? What are the pro's/con's? Is there one that stand out as the leader?
Thanks
If it's not broken, fix it until it is
|
|
|
|
|
In fact, WPF itself is a framework (more exactly: It is a graphics framework and part of the even bigger .Net framework [Which is a definition for the .Net framework classes, ASP.Net, SilverLight and WinForms technologies - I am sure there are many others]).
Therefore the .Net framework is the only framework providing "The real WPF", and this leads to the cunclusion that it is the outstanding leader.
On Linux there is the Mono framework as an adaption of the .Net framework, but it is not that wide-spread and I am not sure wether it provides support for WPF.
You know the world is going crazy when the best rapper is a white guy, the best golfer is a black guy, the tallest guy in the NBA is Chinese, the Swiss hold the America's Cup, France is accusing the U.S. of arrogance, Germany doesn't want to go to war, and the three most powerful men in America are named "Bush", "Dick", and "Colon."
|
|
|
|
|
Are you talking about MVVM frameworks? Or are you referring to something like Prism? Application frameworks? This is a very open ended question that is impossible to answer in its current form.
|
|
|
|
|
Are you talking about an MVVM framework? DI / container engine? etc. You're going to get a million different answers. Personally, I wrote my own lifting various parts from various places. I don't know of any (other then my own) that have MVVM stuff + DI + UI rolled into one as those pieces are usually "sold separately". I'd do the same thing if I were you as you almost never need to use the advanced parts of the open source libraries and good luck getting support .
|
|
|
|
|
Prism[^] is a good framework to explore. When setup correctly, Prism and MVVM can complement each other.
|
|
|
|
|
I have a main window that contains buttons to open forms (other windows) for registration. How to open these windows forms inside the main window, the main window active mantento?! I tried with child, but on the menu bar of the windows icon appears for each open form.
|
|
|
|
|
It may be worth learning something of model view presenter(MVP) - this would allow you to display different views within the same main window.
A good introduction can be gained from going through - Sams Teach Yourself WPF in 24 Hours - I found it invaluable in taking me step by step through the process of creating WPF MVP applications.
[edit - corrected typo and grammar]
“That which can be asserted without evidence, can be dismissed without evidence.”
― Christopher Hitchens
modified 16-Oct-13 11:52am.
|
|
|
|
|
If you want to prevent the child icons appearing in the TaskBar, set ShowInTaskBar="False" in the Window section of those forms.
|
|
|
|
|
Being new to WPF I am starting to like the databinding aspect and have a noob question.
If possible I would like to update all elements on the UI via databinding - in some cases I have found this to be very difficult and have resorted to the dispatcher class.
So my question is this - is it possible and desirable to use databinding for everything on the UI or is it considered reasonable to make use of despatcher at times?
Thanks
“That which can be asserted without evidence, can be dismissed without evidence.”
― Christopher Hitchens
|
|
|
|
|
I assume You are using a ViewModel. If so then set the dataContext and then the binding is simple.
Hope this helps.
David
|
|
|
|
|
Thanks
That helped, I had tried it before but had not set up the bound variable as a property.
I changed it to a property, set the datacontext and notify, and it worked!
“That which can be asserted without evidence, can be dismissed without evidence.”
― Christopher Hitchens
|
|
|
|