How to add designmode dummy data into WPF control - wpf

Added entity Dummy to the usercontrol resources but Visual Studio complains it can't find resource 'Dummy'. Is it possible to add design data this way? What am I doing wrong?
<UserControl x:Class="MovieScraper.Media"
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:entity="clr-namespace:Processor.Entity;assembly=Processor"
mc:Ignorable="d"
d:DataContext="{StaticResource ResourceKey=Dummy}"
d:DesignHeight="300" d:DesignWidth="300" Background="White">
<UserControl.Resources>
<entity:Media x:Key="Dummy" Title="Akira"></entity:Media>
</UserControl.Resources>
<Grid>
<TextBlock Text="{Binding Title}"></TextBlock>
</Grid>
</UserControl>

I would create a MockDataContext model. This works pretty nice. This is a snippet from code that I have:
<UserControl x:Class="Modules.Core.Views.HeaderView"
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:Modules.Core.ViewModels"
mc:Ignorable="d"
d:DesignHeight="100" d:DesignWidth="1024">
<Grid d:DataContext="{d:DesignInstance Type=viewModels:MockHeaderViewModel, IsDesignTimeCreatable=True}">
</Grid>
</UserControl>
Big advantage is that you can actually run some code. For example I use it to update the time in my header in design time and vary some fields. You can immediately see if your binding work and your layout is giving you components enough space.

You are trying to bind d:DataContext to resource Dummy, but you are missing the keyword Binding. Please change this line to
d:DataContext="{Binding Source={StaticResource Dummy}}

Related

Can I use a resource declared in its own XXX.Resources?

I've a Window, in which I declare one Resource, I was hopping to be able to use it in the window itself, but I'm not sure this is possible?
<dx:ThemedWindow x:Class="SomeWindow"
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:dxco="http://schemas.devexpress.com/winfx/2008/xaml/controls"
xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm"
xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
xmlns:frameworkExtensions="clr-namespace:SomeNamespace.FrameworkExtensions"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800" ContentTemplate="{StaticResource dataTemplate}" >
<dx:ThemedWindow.Resources>
<frameworkExtensions:ConventionBasedDataTemplateSelector x:Key="dataTemplate"/>
</dx:ThemedWindow.Resources>
[...]
</dx:ThemedWindow>
Here by example it's for a custom DataTemplate.
How should I do this? Because currently Visual Studio is warning me that it can't find "dataTemplate"
Your problem is because you're trying to use a resource before it's there.
Let's consider a simple example.
If you did:
<Window x:Class="WpfApp1.MainWindow"
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"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="200" Width="400"
>
<Window.Resources>
<SolidColorBrush x:Key="TestResourceBrush" Color="Blue"/>
</Window.Resources>
<Window.Background>
<StaticResource ResourceKey="TestResourceBrush"/>
</Window.Background>
<Grid>
</Grid>
</Window>
Then you see no error and the background of the window turns blue.
If you instead do:
<Window x:Class="WpfApp1.MainWindow"
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"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="200" Width="400"
Background="{StaticResource TestResourceBrush}"
>
<Window.Resources>
<SolidColorBrush x:Key="TestResourceBrush" Color="Blue"/>
</Window.Resources>
<Grid>
</Grid>
</Window>
Then that will error, saying it can't resolve the resource.
If you made that a DynamicResource, then that works ok.
<Window x:Class="WpfApp1.MainWindow"
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"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="200" Width="400"
Background="{DynamicResource TestResourceBrush}"
>
<Window.Resources>
<SolidColorBrush x:Key="TestResourceBrush" Color="Blue"/>
</Window.Resources>
<Grid>
</Grid>
</Window>
The way resources work is that the control looks for the resource in itsself, then in the parent then all the way up the visual tree until it hits the application. see here
so if you had the resource declared on a listview then the panel it is contained within can't use the resource but the list view can.
i suspect the issue you're having is that the template doesn't exist yet because you haven't compiled your code. because wpf is so flexible the designer actually has to run the code to see how it works so if your code hasn't been compiled or has changed since the last compilation the designer can't see the changes.
failing that it may just be order of execution, ie your consuming your template in an attribute but creating it in a child element, in this case move the contentTemplate to an element below the resources element and you should have access to it.
ie
<dx:ThemedWindow.Resources>
<frameworkExtensions:ConventionBasedDataTemplateSelector x:Key="dataTemplate"/>
</dx:ThemedWindow.Resources>
<dx:ThemedWindow.ContentTemplate>
<StaticResource ResourceKey="dataTemplate"/>
</dx:ThemedWindow.ContentTemplate>

Attaching CollectionViewType to a CollectionViewSource

This might be simple but I do not know how to go about it. I want to attach a CollectionViewType like this: CollectionViewType="ListCollectionView" to a CollectionViewSource. I am using a typed DataSet and the CollectionViewSource was generated through a Drag and Drop. I am using a DataSet and not Entity Framework.
I have put the following references:
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
However, I am failing to modify the CollectionViewSorce accordingly.
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Title="MainWindow"
Width="500"
Height="400"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
>
<Window.Resources>
<local:DataSet1 x:Key="DataSet1" />
<CollectionViewSource x:Key="ExpenseDescriptionsViewSource" Source="{Binding ExpenseDescriptions, Source={StaticResource DataSet1}}" />
</Window.Resources>
<Grid >
</Grid>
</Window>
All the examples I have seen deal with Entity Framework and not with DataSet.
How can I implement CollectionViewType changes in the CollectionViewSource above using DataSset?

WPF Adding UserControl to RadRibbonTab

I'm learning to use WPF.
I'm trying to make RadRibonTab in one file and it's content in the other, however i'm unable to do that.
My parent UserControl:
<UserControl x:Class="TelerikTotorial2.RibbonBar.RibonBarView"
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:telerik="http://schemas.telerik.com/2008/xaml/presentation"
xmlns:historyTab="clr-namespace:TelerikTotorial2.RibbonBar.History"
mc:Ignorable="d"
>
<telerik:RadRichTextBoxRibbonUI ApplicationButtonContent="File" CollapseThresholdSize="50,50" VerticalAlignment="Top" Margin="0,0,0,-0.2">
<telerik:RadRibbonTab Header="History" HorizontalAlignment="Left" Margin="0" Width="107.2">
<historyTab:HistoryTabView/>
</telerik:RadRibbonTab>
</telerik:RadRichTextBoxRibbonUI>
</UserControl>
UserControl which I try to add
<UserControl x:Class="TelerikTotorial2.RibbonBar.History.HistoryTabView"
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:telerik="http://schemas.telerik.com/2008/xaml/presentation"
xmlns:history="clr-namespace:TelerikTotorial2.RibbonBar.History"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<telerik:RadRibbonGroup Header="Tools">
<telerik:RadRibbonButton Text="View" Size="Large"/>
</telerik:RadRibbonGroup></UserControl>
However I get exception "InvalidOperationException: Specified element is already the logical child of another element.".
If i replace <historyTab:HistoryTabView/> with
<telerik:RadRibbonGroup Header="Tools">
<telerik:RadRibbonButton Text="View" Size="Large"/>
</telerik:RadRibbonGroup>
then everything is working, however I want to have this code in separate files. Perhaps someone can explain what I am doing wrong? Perhaps it isn't possible to add UserControl to other UserControl?

Moving a part of xaml file to another xaml file

In my project, I have XAML file like this:
<Grid Margin="50,0,0,0">
//Huge amount of code goes here
</Grid>
It is very difficult to go through all the code in Grid while designing. Can I move all the code to a separate XAML file file and in in this Grid content i will call that XAML file??
<Grid Margin="50,0,0,0">
//call xaml file here
</Grid>
You should define a UserControl; assume the "Huge amount of code" is sth like this:
<Border Name="yourBorder">
//Other Xamls
</Border>
Now you create a new UserControl an put this Border in it
<UserControl x:Class="WpfApplication.UserControl1"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Border Name="yourBorder">
//Other Xamls
</Border>
</UserControl>
You can use UserControl1 in other Xamls. You should add xmlns:wp="clr-namespace:WpfApplication" to your Xaml. For example if you want to use it in a Window:
<Window x:Class="WpfApplication.Window2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wp="clr-namespace:WpfApplicationUpper" //This is what I mentioned
Title="Window2" Height="300" Width="300">
<StackPanel>
<wp:UserControl1 /> // You call it using this format
</SatckPanel>

Changing Silverlight view

I am trying to create a extension from an existing view where I have a list of text controls and in the new project I would like to use this control and change one of these text boxes to a radio button.
Using MVVM makes it easy to use the same code without having duplicated code, but for the XAML view I find no good way to do this change without creating a copy.
Example:
Core project with the main user control
<UserControl x:Class="SilverlightApplication4.MainPage" Name="test"
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"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White">
<StackPanel>
<TextBlock>
asdfasdf
this is a test
</TextBlock>
<Button Height="120" Name="asdf" Content="This is a Button">
</Button>
</StackPanel>
</Grid>
</UserControl>
Now I have a second project where I want to change the TextBlock to something else.
<UserControl x:Class="SilverlightApplication1.MainPage"
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"
xmlns:core="clr-namespace:SilverlightApplication4;assembly=SilverlightApplication4" d:DesignHeight="300" d:DesignWidth="400">
<core:MainPage>
<!-- how do i change the type of child elements?-->
</core:MainPage>
</UserControl>
Solution will be to use a ContentControl and bind it to a ViewModel through Caliburn.Micro.
I added an example and code to my other question (below the XAML section): Project structure for EF/Silverlight application

Resources