Hi, I have a problem using the MVVM pattern after I instantiate my view model in this way:
<phone:PhoneApplicationPage.Resources>
<local:DetailVM x:Key="DetailVM"/>
</phone:PhoneApplicationPage.Resources>
How do I know when this Page is Loaded?
You can use the Blend SDK and add an event trigger that triggers a command in your viewmodel.
include
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
in your xmlns includes, and then add a trigger for the Loaded event.
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<i:InvokeCommandAction Command="{Binding LoadCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
The LoadCommand is simply a property that returns an ICommand. You should of course either set the DataContext of the page to your viewmodel, or set the source of the binding to the one in your resources.
Related
I have an UWP application with ReactiveUI. I have the following in my DataTemplate:
<MenuFlyoutItem Icon="Edit"
Text="{Binding Resources[EditLabel]}">
Since it is a template, I can't access them in my code behind. So how can I setup the binding here?
I tried the Command but then I can only bind to the item itself and not to the ViewModel of the containing view.
EDIT: I adjusted it that way:
<MenuFlyoutItem Icon="Edit"
Text="{Binding Resources[EditLabel]}"
Command="{Binding DataContext.EditAccountCommand, ElementName=AccountList}"
The Binding does work. I wanted now to add a Converter so I can get the clicked object from the eventargs. But when I add a converter it is executed when the page appears and not when the click is executed. Also I do get the bound command instead of the click arguments.
in my Windows Phone application I have following scenario:
I have two ListBox'es f.e lbx1 and lbx2
ViewModel loads data to lbx1
User tap on ListBox, command is fired, ViewModel set visibility of lbx1 to collapsed and lbx2 to visilbe.
Now I want to do following thing: when lbx2 is visible I want to intercept OnBackKeyPress event in My ViewModel so that I can change visibility of lbx1 to visible and lbx2 to collapsed.
Is it even possible with mvvm.light ?
Ok, found my aswer:
In page.xaml insert:
<i:Interaction.Triggers>
<i:EventTrigger
EventName="BackKeyPress">
<GalaSoft_MvvmLight_Command:EventToCommand
Command="{Binding BackKeyPressCommand}"
PassEventArgsToCommand="True"
/>
</i:EventTrigger>
</i:Interaction.Triggers>
Where Command is my ViewModel command, and PassEventArgsToCommand="True" enables me to cancel event when event is fired
I've managed to get the LongListSelector running through MVVM.
In other words the ItemSource is set through a property on my viewmodel.
But for some weird reason, I can't seem to be able to 'bind' the SelectedItem of the LongListSelector... I'm not getting in the Set nor Get of the ViewModel property.
How is this done? And what should the 'type' of the SelectedItem on the ViewModel be? I thought the Type of the Class inside the Group?
My current xaml:
<silverlighttoolkit:LongListSelector x:Name="AlbumsList"
Background="Transparent"
ItemTemplate="{StaticResource ItemTemplate}"
GroupHeaderTemplate="{StaticResource GroupHeaderTemplate}"
GroupItemTemplate="{StaticResource GroupItemTemplate}"
ItemsSource="{Binding GroupedAlbums}"
SelectedItem="{Binding SelectedAlbum, Mode=TwoWay}">
<silverlighttoolkit:LongListSelector.GroupItemsPanel>
<ItemsPanelTemplate>
<silverlighttoolkit:WrapPanel />
</ItemsPanelTemplate>
</silverlighttoolkit:LongListSelector.GroupItemsPanel>
</silverlighttoolkit:LongListSelector>
Use the SelectionChanged event. Either though a EventToCommand behaviour, or a attached behaviour.
I'm trying to use the ContextMenu control in WP7 with Caliburn Micro. I used the following example:
http://compiledexperience.com/blog/posts/wp7-context-menus-with-caliburn-micro
I have a "PurchaseListViewModel" approach (second scenario in the example).
Now my problem:
How can I make the menu items localizable. I tried to use the following:
Bind to the resource file which is exposed in the BaseViewModel (works other places in the xaml):
Header="{Binding Path=LocalizedResources.MenuDelete,
Source={StaticResource localizedStrings}}"
Bind against a property in the ViewModel
Header="{Binding MenuDeleteText}"
Both approaches didn't work; no text is shown. What am I doing wrong?
Did you tried
Header="{Binding LocalizedResources.MenuDelete,
Source={StaticResource localizedStrings}}"
FYI I'm pretty new to Silverlight.
Okay, so I want to build a simple user control that contains a button plus some additional XAML as specified by the client of the control.
I got searching on Google and found at least 30 different articles that were all very confusing; especially because they talk about styling animation, customizing other controls that you don't own, and other crap I'm not ready for yet.
This is what I did.
In VS 2010, I right clicked and added a new UserControl called MyControl
To the MyControl.xaml I changed the LayoutRoot to a StackPanel and added a Button inside it
In my MainPage.xaml I added an instance of MyControl
I added a TextBox as a child element of this instance
I tried to build and got an error that MyControl didn't support Direct Content
Googled some more..
I changed MyControl to inherit from ContentControl and updated the xaml
I added a ContentPresenter in the xaml to represent the client "custom content"
Okay, it builds and the TextBox shows up, but the Button is missing.
Here's the relevant section from MainPage.xaml
<my:MyControl HorizontalAlignment="Left" Margin="49,26,0,0" x:Name="myContentControl1" VerticalAlignment="Top" Height="550" Width="389">
<TextBox Height="72" HorizontalAlignment="Left" Margin="166,339,0,0" Name="textBox1" Text="TextBox" VerticalAlignment="Top" Width="460" />
</my:MyControl>
Here's the MyControl.xaml
<ContentControl x:Class="ContentControlTest.MyControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
d:DesignHeight="480" d:DesignWidth="480">
<StackPanel x:Name="LayoutRoot" Background="{StaticResource PhoneChromeBrush}" Orientation="Vertical">
<ContentPresenter/>
<Button Content="Button" Height="72" HorizontalAlignment="Left" Margin="78,254,0,0" Name="FooFoo" VerticalAlignment="Bottom" Width="160" />
</StackPanel>
</ContentControl>
And here is the MyControl.cs
using System.Windows.Controls;
namespace ContentControlTest
{
public partial class MyControl : ContentControl
{
public MyControl()
{
InitializeComponent();
}
}
}
The way I thought it worked was that the child elements of the control instance are set as the Content property on the ContentControl base class of MyControl. Then, ContentPresenter "pastes" that content into the MyControl.xaml wherever appears.
Although that does seem to be how it works, in the process it is "eating" the Button that I have defined in the MyControl.xaml.
I'm trying not to get into ControlTemplate etc that at this point unless it is absolutely necessary.
Can someone with a clue please tell me what I am doing wrong.
thanks!
That's because the Content of the control is the entire StackPanel you've written by hand; when you set a new Content, the StackPanel is replaced.
A ControlTemplate is necessary for this scenario, I think; it would be a very simple one after all. The starting point can be the default style of the content control; put the style inside a ResourceDictionary (for instance, in the <ContentControl.Resources> section of your user control), and you're ready to go; all you need to do is add a grid and button inside that template.
Note that the style I linked to sets itself as the default for any reachable ContentControl; to make it only apply to your control and not to any children that may appear inside it, add x:Key="someKey" to the Style and set the ContentControl's Style property explicitly to Style={StaticResource someKey}.
Let me know if you need additional information; also, I might be wrong and there may be an easier way, but I doubt it; the Content property is meant to behave exactly like what you described.