I'm attempting to use EventToCommand to initialize my ViewModel, but the command isn't firing. I suspect it's because the Triggers section is not within the databound container, but how can I do that in my example? I'm trying to stick to straight XAML if possible.
<Window x:Class="MVVMSample.Home"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:viewModels="clr-namespace:MVVMSample.ViewModels"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF4"
d:DataContext="{d:DesignInstance Type=viewModels:HomeViewModel, IsDesignTimeCreatable=True}"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<viewModels:HomeViewModel x:Key="ViewModel" x:Name="ViewModel" />
</Window.Resources>
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<cmd:EventToCommand Command="{Binding LoadedCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
<Grid DataContext="{StaticResource ViewModel}">
<TextBlock Text="{Binding PersonCount}" />
</Grid>
</Window>
You are right, the datacontext is part of the problem,but I would solve it by using mvvm-light as it was designed.
If you are using MVVM_Light then your should use the view model locator. Its the main backbone of the framework. I used mvvm light to learn about the mvvm principle. I liked it a lot because it was simple and allowed me to learn with as quick a learning curve as possible.
In mvvm-light you declare your viewmodellocator in your app.xaml
<Application.Resources>
<ResourceDictionary>
<!--Global View Model Locator-->
<vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />
</ResourceDictionary>
</Application.Resources>
then in your view (be it a usercontrol or window etc) you "attach" you viewmodel to your view as follows: Notice the DataContext declaration.
<UserControl x:Class="FTC.View.TrackingListView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:cmd="http://www.galasoft.ch/mvvmlight"
mc:Ignorable="d"
DataContext="{Binding YourViewModel, Source={StaticResource Locator}}"
d:DesignHeight="700" d:DesignWidth="1000">
This way the view model locator from mvvm light can either create a singleton of your viewmodel or a unique instance as you need. It can also use IOC to inject services into the constructor of your viewmodel.
So for example, if I have a viewmodel that deals with people objects from a datamodel, i create a people service that performs the CRUD operation and then reference it in the viewmodel constructor parameters. This allows me to use either fake design time data or real data from the model. It also keep all concerns decoupled, which is the purpose of mvvm.
I recommend reading more about the MVVM-light framwork and building one of the samples from thier site a galasoft.
See this video
hope this helps
Related
I am creating a custom panel which is basically just a fancier StackPanel with some added functionality. I was going to use a UserControl which contains a StackPanel but I don't know how to make my UserControl accept content to fill it's StackPanel.
This is what I'm trying:
<UserControl
x:Class="transformations.fold_panel"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:transformations"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<StackPanel Orientation="Vertical">
<Button Content="First" />
<ContentControl Content="{x:Bind Content, Mode=OneWay}"></ContentControl>
</StackPanel>
</UserControl>
Usage:
<local:fold_panel>
<Button Content="Second" />
</local:fold_panel>
When I try this I get the following error:
WinRT originate error - 0x80070057 : 'The parameter is incorrect.'.
You can't bind the Content of a StackPanel in a UserControl's Content to the Content property of the same UserControl. This will introduce a circular reference.
In your example, the Content property of the fold_panel UserControl will be set to the StackPanel that you defined in the XAML markup.
If you want to be able to set the Content of the ContentControl in the StackPanel, you should add a custom dependency property to the fold_panel class and bind the Content property of the ContentControl to this one:
<ContentControl Content="{x:Bind CustomContent, Mode=OneWay}" />
You can then set your custom property something like this:
<local:fold_panel>
<local:fold_panel.CustomContent>
<Button Content="Second" />
<local:fold_panel.CustomContent>
</local:fold_panel>
But if you really want a custom StackPanel, you should create a class that inherits from StackPanel rather than UserControl.
this code work correct:
<UserControl x:Class="Extended.InputControls.TextBoxUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Extended.InputControls">
<TextBox x:Name="textBox"
ToolTip="{Binding Path=CustomToolTip,RelativeSource={RelativeSource AncestorType=local:TextBoxUserControl}}"/>
</UserControl>
but this code Does not work!!!
<UserControl x:Class="Extended.InputControls.TextBoxUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Extended.InputControls">
<TextBox x:Name="textBox">
<TextBox.ToolTip>
<ToolTip Text="{Binding Path=CustomToolTip,RelativeSource={RelativeSource AncestorType=local:TextBoxUserControl}}" Background="Yellow"/>
</TextBox.ToolTip>
</TextBox>
</UserControl>
i need create custom tooltip and bind it to CustomToolTip but in second code that's not bind to anything
where is the problem?
First of all, if we're talking WPF here, it should be <ToolTip Content="..."> instead of <ToolTip Text="...">, since ToolTip has no Text property.
Regarding the binding: Binding to other elements in the user control from within a ToolTip doesn't work since ToolTip elements are not part of the visual tree, as explained in another question that also provides one potential solution.
However, it seems that you're binding to some property defined in the UserControl's code-behind? In that case it's even easier to solve by setting the UserControl's DataContext to the control itself:
<UserControl x:Class="Extended.InputControls.TextBoxUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Extended.InputControls"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<TextBox x:Name="textBox">
<TextBox.ToolTip>
<ToolTip Content="{Binding CustomToolTip}" Background="Yellow"/>
</TextBox.ToolTip>
</TextBox>
</UserControl>
Alternatively, you could also set the DataContext in code-behind:
public TextBoxUserControl()
{
this.DataContext = this;
InitializeComponent();
}
In both cases, the CustomToolTip property can be accessed directly without the need for a RelativeSource binding.
An even better solution would be to introduce come kind of Viewmodel class that holds the CustomToolTip and all similar properties, and set this class as the UserControl's DataContext.
It seems like every object that has been added to a page in a Windows 8 App gets this "slide from right to left"-entrance-transition which begins whenever someone navigates to the page.
Is there a possibility to remove single objects from this transition?
Neither
<Object.Transitions>
<TransitionCollection />
</Object.Transitions>
nor this thread helped...
Any ideas?
AFAIK There's no way to exempt a given object from the transitions applied by it's parents. The only thing I can suggest is to structure your xaml in such a way that the transition isn't applied. By this I mean having this special item in a panel that has no children transitions while the rest of the page is in a panel with a child transition. Depending on where this item is it could be very easy or difficult as hell.
As Nigel suggested, the only solution I have found has been to change the page structure and put the elements out of the grid that has the animations:
<Page
x:Class="App1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.Transitions>
<TransitionCollection/>
<!-- Ensure no transitions in background -->
</Grid.Transitions>
<TextBlock FontSize="50" Margin="50">This item is not animated</TextBlock>
<Grid>
<Grid.ChildrenTransitions>
<TransitionCollection>
<!-- add transitions here -->
<EntranceThemeTransition FromVerticalOffset="1500" FromHorizontalOffset="1500"/>
</TransitionCollection>
</Grid.ChildrenTransitions>
<TextBlock Margin="50,100" FontSize="50">This item is animated</TextBlock>
</Grid>
<TextBlock FontSize="50" Margin="50,150">Another not animated item</TextBlock>
</Grid>
</Page>
I'm getting this exception when I run my app. I navigate to a XAML and the Debugger exits out with this message:
DrawingSurfaceBackgroundGrid must be the first element to begin
drawing.
On my MainPage.xaml, I navigate to this DirectX.xaml file when a button is clicked on MainPage.xaml. I made sure the DrawingSurfaceBackgroundGrid is the only control, so it's the FIRST element. Did I get the order wrong? (Also shown here are my App.xaml and MainPage.xaml)
DirectX.xaml
<phone:PhoneApplicationPage
x:Class="GameWp8Dx.Hud.Tests.DirectX"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d"
shell:SystemTray.IsVisible="True">
<!--LayoutRoot is the root grid where all page content is placed-->
<DrawingSurfaceBackgroundGrid x:Name="DrawingSurfaceBackground" Loaded="DrawingSurfaceBackground_Loaded">
</DrawingSurfaceBackgroundGrid>
</phone:PhoneApplicationPage>
MainPage.xaml
<phone:PhoneApplicationPage
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
xmlns:local="clr-namespace:GameWp8Dx"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" xmlns:ec="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions"
x:Class="GameWp8Dx.MainPage"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True"
local:TiltEffect.IsTiltEnabled="True"
ApplicationBar="{StaticResource socialAppBar}"
>
<!--Transitions-->
<toolkit:TransitionService.NavigationInTransition>
<toolkit:NavigationInTransition>
<toolkit:NavigationInTransition.Backward>
<toolkit:TurnstileTransition Mode="BackwardIn"/>
</toolkit:NavigationInTransition.Backward>
<toolkit:NavigationInTransition.Forward>
<toolkit:TurnstileTransition Mode="ForwardIn"/>
</toolkit:NavigationInTransition.Forward>
</toolkit:NavigationInTransition>
</toolkit:TransitionService.NavigationInTransition>
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot">
<phone:Panorama x:Name="test" HorizontalAlignment="Left" Height="686" VerticalAlignment="Top" Width="470"
Title="{Binding Path=LocalizedResources.ApplicationTitle, Source={StaticResource ResourceKey=LocalizedStrings}}" TitleTemplate="{StaticResource MainPageTitleTemplate}" HeaderTemplate="{StaticResource MainPageHeaderTemplate}">
App.xaml
<Application
x:Class="GameWp8Dx.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone">
<!--Application Resources-->
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="CustomStyles.xaml"/>
</ResourceDictionary.MergedDictionaries>
<local:LocalizedStrings xmlns:local="clr-namespace:GameWp8Dx" x:Key="LocalizedStrings"/>
<shell:ApplicationBar x:Key="inGameAppBar" IsVisible="True" IsMenuEnabled="False">
<shell:ApplicationBarIconButton IconUri="/Assets/Icons/microphone.png" Text="record" Click="record_Click"/>
<shell:ApplicationBarIconButton IconUri="/Assets/Icons/questionmark.png" Text="info"/>
<shell:ApplicationBarIconButton IconUri="/Assets/Icons/map.png" Text="map" Click="map_Click"/>
</shell:ApplicationBar>
<shell:ApplicationBar x:Key="socialAppBar" IsVisible="True" IsMenuEnabled="False">
<shell:ApplicationBarIconButton IconUri="/Assets/Icons/appbar.twitter.png" Text="Tweet" Click="OnTweet"/>
<shell:ApplicationBarIconButton IconUri="/Assets/Icons/appbar.twitter.bird.png" Text="Tweet" Click="OnTweetSharp"/>
<shell:ApplicationBarIconButton IconUri="/Assets/Icons/appbar.facebook.png" Text="Share" Click="OnShare"/>
<shell:ApplicationBarIconButton IconUri="/Assets/Icons/appbar.facebook.heart.png" Text="Like" Click="OnLike"/>
</shell:ApplicationBar>
</ResourceDictionary>
</Application.Resources>
<Application.ApplicationLifetimeObjects>
<!--Required object that handles lifetime events for the application-->
<shell:PhoneApplicationService
Launching="Application_Launching" Closing="Application_Closing"
Activated="Application_Activated" Deactivated="Application_Deactivated"/>
</Application.ApplicationLifetimeObjects>
</Application>
UPDATE
Turns out TransitionFrame() is the culprit! Changed it back to PhoneApplicationPage() and DrawingSurfaceBackgroundGrid works without a hitch.
App.xaml.cs
private void InitializePhoneApplication()
{
if (phoneApplicationInitialized)
return;
// Create the frame but don't set it as RootVisual yet; this allows the splash
// screen to remain active until the application is ready to render.
RootFrame = new TransitionFrame();
//RootFrame = new PhoneApplicationFrame();
RootFrame.Navigated += CompleteInitializePhoneApplication;
// Handle navigation failures
RootFrame.NavigationFailed += RootFrame_NavigationFailed;
// Handle reset requests for clearing the backstack
RootFrame.Navigated += CheckForResetNavigation;
// Ensure we don't initialize again
phoneApplicationInitialized = true;
}
I'm just guessing here, but did you change the RootFrame to a special TransitionFrame? If so, I'm assuming that doesn't work well with DrawingSurfaceBackgroundGrid. You can probably change to a DrarwingSurface and resolve this issue. However DrawingSurface has an extra overdraw of all DirectX content shown on the screen due to an intermediary drawing layer used to interweave with XAML. So using DrawingSurface on full page D3D content will likely present performance problem (even more so on HD resolution screens).
To understand the problem , try thinking through what happens if you're navigating away from a page with DrawingSufaceBackgroundGrid? Should it animate? If so, you need XAML animations to be applied on D3D content and that's not supported for DrawingSurfaceBackgroundGrid. It is supported for DrawingSurface but at the heavy cost of having an intermediary drawing layer used to interweave with XAML.
I think in the tradeoff between bad DirectX perf and lacking page transitions, I'd opt out of having page transitions. That being said, different apps will have different compromises.
As another thought, you might be able to switch between a standard PhoneApplicationFrame and a TransitionFrame based on whether you need full screen D3D or not. I haven't tested something like that though.
i would like to create a custom base class for some of my UserControls.
Doing this in VS2008 is fine and compiles as expected, but when i edit the control in Blend 3 the base class in the blabla.g.vb is always changed back to System.Windows.Controls.UserControl.
How can i force Blend to keep the assigned base class?
regards
Christoph
Can you show your XAML?
I suspect the case is your XAML is like this:
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:SilverlightApplication15"
x:Class="SilverlightApplication15.MainPage"
Width="640"
Height="480">
<Grid x:Name="LayoutRoot"
Background="#FF313131" />
</UserControl>
When it should be something like:
<local:BlahBlah xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:SilverlightApplication15"
x:Class="SilverlightApplication15.MainPage"
Width="640"
Height="480">
<Grid x:Name="LayoutRoot"
Background="#FF313131" />
</local:BlahBlah>
The .g.vb file is generated from the XAML so it's not a file you should edit directly.