Changing Silverlight view - silverlight

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

Related

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?

WPF UserControl: Use default WPF style rather than inheriting style of parent application

I have a WPF User Control embedded in an application, which is inheriting the style of the application. I designed the User Control with the default WPF style in mind and it doesn't define its own resources/styles. However, as a result of inheriting the style of the host application, it doesn't look correct. I'd prefer if the User Control just used the default WPF style. Is there a simple way I can do this?
The UserControl itself consists of a System.Windows.Controls.Frame, which references a System.Windows.Control.Page:
<UserControl x:Class="OrderParcelAddInPrototype.WpfControls.Controls.OrderParcelControl"
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="450">
<Frame Source="/OrderParcelAddInPrototype.WpfControls;component/Pages/MainPage.xaml" />
</UserControl>
Neither the UserControl or the Page define their own styles.
<Page x:Class="OrderParcelAddInPrototype.WpfControls.Pages.StartPage"
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="450" Title="Main Page">
<ScrollViewer VerticalScrollBarVisibility="Auto">
<StackPanel VerticalAlignment="Top" MaxWidth="350" CanVerticallyScroll="True"
ScrollViewer.CanContentScroll="True">
<!-- Removed for brevity -->
</StackPanel>
</ScrollViewer>
</Page>
Define your usercontrol style inside usercontrol resources and apply that style in user control
<UserControl x:Class="MyNamespace.MyUserControl"
...
Style="{DynamicResource ResourceKey=MyUserControlStyle}">
<UserControl.Resources>
...
<Style x:Key="MyUserControlStyle" TargetType="{x:Type UserControl}">
</Style>
</UserControl.Resources>
otherwise, you can merge resource dictionary of your usercontrol by using
mergeddictionary in resource dictionary in app.xaml

How to add designmode dummy data into WPF control

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}}

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>

Template of root control

I have custom control named Frame (public class Frame : ContentControl, IDisposable). Frame in constructor defines DefaultStyleKey = typeof(Frame); and Frame template is in Generic.xaml resource. Now In some other project I use frame that is in Silverlight.Controls and if I set my main page that the root element is frame like this
<ShellFrame:Frame x:Class="Modules.Adresar.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:s="clr-namespace:Modules.Adresar.ViewModel"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:ShellFrame="clr-namespace:Silverlight.Controls;assembly=Silverlight.Controls" mc:Ignorable="d"
DataContext="{Binding Source={StaticResource VMLocator}, Converter={StaticResource VMIndexerConverter}, ConverterParameter=AdresarVM}"
d:DataContext="{d:DesignInstance IsDesignTimeCreatable=True, Type=s:AdresarViewModel}" x:Name="MainFrame">
<Grid x:Name="LayoutMain">
</Grid>
I can't edit template of frame. Blend is showing al edit template menu items disabled. But If I put frame inside layout root grid in normal user control then everything is ok.. like this
<UserControl
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:ShellFrame="clr-namespace:Silverlight.Controls;assembly=Silverlight.Controls"
mc:Ignorable="d"
x:Class="Adresar.Test"
d:DesignWidth="640" d:DesignHeight="480">
<Grid x:Name="LayoutRoot">
<ShellFrame:Frame Content="Frame" HorizontalAlignment="Left" Margin="72,136,0,0" VerticalAlignment="Top"/>
</Grid>
Does enybody knows why this is happening? Thank you!
Changing your Frame control to be based on UserControl instead of ContentControl should fix the behavior you're seeing in Blend. I'm not sure how you're using your control, but if you can't utilize a UserControl you might want to include some context.

Resources