|
Here are a few more thoughts..
You don't need any of the aforementioned libraries to implement MVVM - it's just without some of their features UI interaction (even something as a simple as displaying a message box) and communication between modules can be difficult.
Another option is WPF Application Foundation. https://waf.codeplex.com
It's not being supported but provides some excellent documentation and samples on a concept of passing a IView interface to the view model:
https://waf.codeplex.com/wikipage?title=Model-View-ViewModel%20Pattern&referringTitle=Home
This allows you to call view (UI) operations from the view model without violating MVVM.
My experience with Prism is version 4, which has been out for years. It's kinda of a all or nothing approach. But Prism 5 breaks it out into separate parts - so you could use the MVVM library and not the Composition library. And as you mentioned it has excellent documentation.
|
|
|
|
|
Yes, in theory you don't need anything. My experience with this is that people that go this way end up in rubbish applications because they fall in exactly the same pitfalls everyone falls into. Then the people that recommended that you didn't need anything aren't there to help you out. Please, for your own sake, pick a framework that fits your application. Don't try to re-invent the wheel, these frameworks / toolkits / whatever you like to call them are out there for years and being used in production for years. Just pick one that you like and have a good feeling with and that fits you use-cases.
If you have a light-weight app, mvvm light can do everything you need and is probably a good option. If you need more (such as authentication, behaviors, nested user controls, resolve by naming convention, etc), pick something that supports that (for example, Catel[^]).
Another tip: don't pick an abandoned project if you want to use if for LoB applications. You'll end up fixing someone else's code that isn't maintained any longer.
|
|
|
|
|
I couldn't disagree more!
My experience is that often people who use a framework spend a great deal of their time trying to find out how to work around ways the framework's limitations in order to do something that would be relatively simple.
Of course, documentation and training is the key - if you and the team are allowed the time to learn a framework properly before using it in a real development, then all can be good, but that learning / experimenting time can equally well be spent learning the basics from the ground up and developing the features your project actually needs.
Another issue with frameworks can be the need to modify the framework in some way in order to do what is required - and then suffering because one cannot update to the latest version of the framework without some angst.
MVVM isn't really that hard - it's far simpler than many of the frameworks out there. And developing your own allows you to pick and choose features from other frameworks at will.
PooperPig - Coming Soon
|
|
|
|
|
Hello!
I have managed to bind a label's Content property to a static property in a class. But when I change the property, the label's content wont update.
XAML code:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Label Content="{Binding Source={x:Static local:MainWindow.Age}}" />
<Button Width="100" Height="100" Click="Button_Click" />
</Grid>
</Window>
C# code:
namespace WpfApplication1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
age = age == 10 ? 11 : 10;
}
static int age = 10;
public static int Age
{
get { return age; }
}
}
}
I use the button to change the property Age between 10 and 11.
Works this bind only when the application starts/loaded?
It would be really neat if I can make this binding update the value in runtime. Btw I use .NET 4.0 and cannot change.
/Steffe
|
|
|
|
|
You need to raise a property changed event that tells WPF that the value it has bound to has changed. WPF isn't magic - it doesn't watch values to see if they have changed, it relies on you the developer telling it that the value has changed. So, to fix this, you need to implement INotifyPropertyChanged like this:
public partial class MainWindow : Window, INotifyPropertyChanged
{
public MainWindow()
{
InitializeComponent();
}
private void Button_click(object sender, RoutedEventArgs e)
{
Age = Age == 10 ? 11 : 10;
}
private static int age = 10;
public static int Age
{
get { return age; }
set
{
if (age == value) return;
age = 10;
RaisePropertyChanged("Age");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler == null) return;
handler(this, new PropertyChangedEventArgs(propertyName));
}
} Now, I don't really see any reasons why your property has to be static. Also, if I were writing this, I'd look at writing this using MVVM, so the logic of maintaining Age would be moved away from the code behind of the view all together.
|
|
|
|
|
You won't be able to call the non-static RaisePropertyChanged event from the setter of a static property.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Doh! Of course not. I was that busy thinking that I wouldn't use a static property that I got stuck in that mindset.
|
|
|
|
|
Pete's answer is almost there, except you can't raise a non-static event from the setter of a static property.
Pete Brown has the solution:
There are two ways to notify the binding system of property changes, both of which follow the same pattern as their instance-based relatives. The first is to create a static event named <propertyName>Changed . The second approach is to create a single event which is the static version of the INPC PropertyChanged event named StaticPropertyChanged .
So in your code, you would either have:
public static event EventHandler AgeChanged;
static int age = 10;
public static int Age
{
get { return age; }
private set
{
age = value;
var handler = AgeChanged;
if (handler != null) handler(null, EventArgs.Empty);
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Age = (Age == 10) ? 11 : 10;
}
or:
public static event EventHandler<PropertyChangedEventArgs> StaticPropertyChanged;
private static void NotifyStaticPropertyChanged(string propertyName)
{
var handler = StaticPropertyChanged;
if (handler != null) handler(null, new PropertyChangedEventArgs(propertyName));
}
static int age = 10;
public static int Age
{
get { return age; }
private set
{
age = value;
NotifyStaticPropertyChanged("Age");
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Age = (Age == 10) ? 11 : 10;
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hi,
I'm building an application which uses a RichTextBox (in WPF). The problem is that the richtextbox is slow, much slower than a TextBox for sure, even slower than WordPad... May I ask you if you have had this issue? I've tried to search for this problem: Microsoft knows it, but it does not seem to solve it as I've been having this problem since I've used RichTextBox for the first time in WPF.
Thanks
Lusvardi Gianmarco
|
|
|
|
|
Sadly, the WPF version of the RTB is mind numbingly slow. Unfortunately there is no known cure for it.
|
|
|
|
|
What about using the winform version in wpf using a WinformHost? Could it be a better solution?
thanks!!
Lusvardi Gianmarco
|
|
|
|
|
It's pretty much a different beast, so you'd have to play around with it to see if it fits your needs.
|
|
|
|
|
I have a listbox with buttons and textblocks in its Data Template.
If I don't first click the list item, clicking the buttons operates on the wrong list item
Can I somehow automatically select the list item from within the data template?
Thanks
If it's not broken, fix it until it is
|
|
|
|
|
You can use the button DataContext instead of the list SelectedItem to obtain the correct object to operate on
Alternatively you can apply an ItemContainerStyle where you attach a PreviewMouseDown event in which you change the SelectedItem
|
|
|
|
|
I have a button who's comman's CanExecute is set to SelectedPhone != null.
When I click the button, the listbox seems to lose focus, and then SelectedItem is null, so the button becomes disabled.
How do I keep the listbox from losing focus, or SelectedItem from becoming null?
Thanks
If it's not broken, fix it until it is
|
|
|
|
|
I have a Company object, and it has a list of Phones on it.
In the UI, I have a Company listbox. The listbox's data template contains company info UI elements, as well as a second listbox with Phone numbers.
How do I set the ItemSource of the Phones listbox to the Company list's Phones collection?
I have this for the Company listbox:
<ListBox Grid.Row="2"
x:Name="AddressList"
ItemsSource="{Binding Addresses}"
SelectedItem="{Binding SelectedAddress}"
BorderBrush="Transparent"
BorderThickness="0"
Visibility="Visible"
Margin="5">
and this for the Phone listbox:
<ListBox Grid.Row="1"
ItemsSource="{Binding DataContext.SelectedAddress.Phones, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"
Background="LightSalmon"
MinHeight="100"
BorderThickness="0"
Margin="5">
Problem is, when the selected item changes on the Company listbox, I see the same phone numbers in each phone section.
What's the right way to do this?
Thanks
If it's not broken, fix it until it is
modified 9-Jun-14 15:51pm.
|
|
|
|
|
I am designing this UI[^].
A location can have one or more addresses. Each address can have one or more Phones, Emails, and Internets. Clicking a location in the tree will load all addresses for that location.
The center "Address" area is a listbox. The ListItem's DataTemplate is comprised of an outer expander with the Street Address area.
Under that are the 3 other expanders for Phones, Emails, and Internets. Each of those expanders contains the Add/Remove buttons and a listbox. The ListBox's Data Template contains the controls for that area.
So far, pretty straight forward.
Ok, now the problem. See this[^]
The Address list is bound to an ObservableCollection<companyinfoheaderentity> called Headers. The CompanyInfoHeaderEntity contains an ObservableCollection<phoneentity> of PhoneEntities called Phones.
It's a typical hierarchical data model.
So why is the SelectedItem of the parent getting data from other Addresses?
Is there a better way to design this?
Thanks
If it's not broken, fix it until it is
|
|
|
|
|
I have a project with me, pls contact me to send the project.
|
|
|
|
|
That is not how it works. No one is going to take a "project" from an unknown source containing who knows what, debug it for you, fix it for you and send it back to you. You need to edit your posting to include a proper description of your problem.
|
|
|
|
|
Hi Chaps (and chapettes)
annoyance - when i am building my WPF app in VS 2013 - i have a form that keeps loading(and displaying) when i build if i have that from's XAML in the designer
whats the fix here?
cheers
Bryce
MCAD
---
|
|
|
|
|
There isn't a fix. This is the way Visual Studio works.
|
|
|
|
|
Switch to a different tab, something other than the XAML, before you build
If it's not broken, fix it until it is
|
|
|
|
|
I have a listbox with a data template[^] with a bunch of controls in it.
When I click a list box item OUTSIDE the template, I get the list item selected. You can see the blue bar around it.
But, if I start by clicking any item INSIDE the data template, the list item is NOT selected.
How do I solve this?
Thanks
If it's not broken, fix it until it is
|
|
|
|
|
I am creating a WPF desktop application to be run on both Windows 7 and Windows 8, and on both desktop computers and tablets. I need to pop up the on-screen keyboard (osk.exe) when a TextBox control GotFocus but only if no physical keyboard is connected.
I am using GetRawInputDeviceList() and GetRawInputDeviceInfo() functions to find the keyboard devices.
When I run my app on the Surface Pro without physical keyboard attached it detects one keyboard with the following information:
Keyboard:
Name = \\?\HID#VID_045E&PID_079C&MI_01&Col03#8&c93f0be&0&0002#{884b96c3-56ef-11d1-bc8c-00a0c91405dd}
Handle: = 18024907
KeyboardMode = 1 | NumKeysTotal = 6 | Subtype = 0 | Type = 81 | VendorID 81 | Usage 0 | ProductID 0 | VersionNumber 1 | NumIndicators 3
When I run it on the Surface Pro with physical keyboard attached it detects two keyboards with the following information:
Keyboard:
Name = \\?\HID#VID_045E&PID_079C&MI_02&Col01#8&17434d04&0&0000#{884b96c3-56ef-11d1-bc8c-00a0c91405dd}
Handle: = 43648689
KeyboardMode = 1 | NumKeysTotal = 154 | Subtype = 0 | Type = 81 | VendorID 81 |Usage 0 | ProductID 0 | VersionNumber 1 | NumIndicators 3
Keyboard:
Name = \\?\HID#VID_045E&PID_079C&MI_01&Col03#8&c93f0be&0&0002#{884b96c3-56ef-11d1-bc8c-00a0c91405dd}
Handle: = 18024907
KeyboardMode = 1 | NumKeysTotal = 6 | Subtype = 0 | Type = 81 | VendorID 81 | Usage 0 | ProductID 0 | VersionNumber 1 | NumIndicators 3
The only difference between these two keyboards is the numberOfKeysTotal field: one is 154, the other is 6.
My question:
How can I tell if a keyboard device is physical or virtual? By NumberOfKeysTotal?
Thanks.
|
|
|
|
|
That's a "feature" of virtual devices that they try to look as "physical" as possible... It may be impossible to differentiate between a virtual and real keyboard.
Some hints to the values you saw:
\\?\HID it is a "Human Interface Device"
VID_045E the Vendor Identifier is 045e (a hexadecimal value)
PID_079C the Product Identifier is 079c (the vendor sets the identifier of their products)
Strange that both keyborads have the same ids... (copy paste error?)
|
|
|
|