Partial declarations, must not specify different base classes? - wpf

When I changed this:
public partial class FrmCategories : UserControl
to this:
public partial class FrmCategories : MyUserControl
Where MyUserControl inherits from UserControl.
I got this error:
Error 2 Partial declarations of 'WpfTest.FrmCategories' must not
specify different base classes
\Projects\WpfTest\WpfTest\FrmCategories.xaml.cs 21 26 WpfTest
XAML:
<UserControl x:Class="WpfTest.FrmCategories"
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:we="clr-namespace:WpfTest"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d" Background="Azure" Height="131" Width="229">
<StackPanel Margin="5,24,5,0" Name="catFrm" Height="75" VerticalAlignment="Top">
I'm beginning WPF (as the name of the project implies), so I expect a trivial error here

You need to change the root element of the XAML file:
<we:MyUserControl x:Class="WpfTest.FrmCategories"
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:we="clr-namespace:WpfTest"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d" Background="Azure" Height="131" Width="229">
<StackPanel Margin="5,24,5,0" Name="catFrm" Height="75" VerticalAlignment="Top">
</we:MyUserControl>

partial only means that you can split the class definition into different files. But still all the partial definitions define only one class which as such may only have one base class.
If you want to inherit from MyUserControl, you have to change the xaml code, too:
<we:MyUserControl x:Class="WpfTest.FrmCategories"
...>
...
</we:MyUserControl>
Also, notice that you only have to specify the base class once, i.e. you could even change the C# code to
public partial class FrmCategories
because the base class has been defined in the xaml.

Related

WPF:programmatically add RibbonBar from UserControl into a RibbonWindow RibbonTab

I have a WPF Ribbon based application and a DLL.
In the application I uses a third party source (Syncfusion) to create my RibbonWindow.
The DLL is a WPF class library that consist of a UserControl which is a RibbonBar.
I would like to add that UserControl to my RibbonWindow.
I'm not sure how to do that programmatically.
MainWindow.xaml.cs
Dynamically loaded the UserControl from the DLL
Assembly asm = Assembly.LoadFile( unitDllPath );
Type typ = asm.GetType( "WX" + ".UserControl1", true, true );
unitDll = Activator.CreateInstance( typ );
MainWindow.xaml
<syncfusion:RibbonTab Name="Tab1" IsChecked="True" >
</syncfusion:RibbonTab>
UserControl.xaml
<syncfusion:RibbonBar Header="Select" Name="Bar1" ></syncfusion:RibbonBar>
UserControl.xaml.cs
public void MainWindow()
{
InitializeComponent();
}
How can I get the RibbonBar from WX.UserControl (Bar1) into (Tab1)?
Thanks.
1)If we need to programmatically add UserControl which is a RibbonBar to your RibbonWindow, your UserControl should be like below
UserControl.xaml
<syncfusion:RibbonBar x:Class="WPFClassLibrary.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:syncfusion="http://schemas.syncfusion.com/wpf"
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">
<syncfusion:RibbonButton SizeForm="Large" Label="New Email"/>
<syncfusion:RibbonButton SizeForm="Large" Label="Inbox"/>
<syncfusion:RibbonButton SizeForm="Large" Label="Drafts"/>
<syncfusion:RibbonButton SizeForm="Large" Label="OutBox"/>
2)Refer the assembly file in your application.
3)use that Assembly in code behind like below
using WPFClassLibrary;
4)now we can call that UserControl from code behind and add to the our RibbonWindow.
tab1.Items.Add(new UserControl1());
i have also attach simple sample for this
http://www.syncfusion.com/downloads/support/directtrac/general/ze/WPfRibbonSample-1343767783

XAML Namespace Beginning with a Number

I have inheritted Button and am trying to add it to my main window.
It doesn't show up in the Toolbox. I have tried rebuilding several times.
I have tried adding xmlns:dc="clr-namespace:123Letters and xmlns:dc="clr-namespace:123Letters;assembly=123Letters to my MainWindow.
My MainWindow is:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="MainWin"
Title="123 Letters">
</Window>
My WeatherStationButton is:
<Button x:Class="WeatherStationButton"
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">
<Grid>
</Grid>
</Button>
and
Public Class WeatherStationButton
Inherits Button
Public Property WeatherStation As tblWeather
End Class
This is super simple stuff. I believe it's because XAML doesn't allow numbers in the first part of namespaces, but I can not find any reference to that anywhere, so I am asking if I am doing something wrong or is this one of XAML's "features"?
Just realized I could add my answer.
If your project name is 123Letters, you need to add an underscore before the first number when referencing it in XAML like, _123Letters.
Your clr-namespace declaration would then be:
xmlns:dc="clr-namespace:_123Letters"
and you could add your inherited button to your MainWindow like this:
<dc:WeatherStationButton x:Name="btnWS" />

Call instance of class in Xaml

Help me understand.
I create template of SilverlightApplication1 in VS2012. Create in my solution folder Models and add class EmployeeTypes.cs. I need to call up the instance of EmployeeTypes.cs in Xaml file (Home.xaml).
I do so in Home.xaml:
<navigation:Page x:Class="SilverlightApplication1.Home"
xmlns:local="clr-namespace:SilverlightApplication1.Models"
....
....
Then i do so ( all this according tutorial http://weblogs.asp.net/psheriff/archive/2012/04/09/silverlight-tree-view-with-multiple-levels.aspx):
<local:EmployeeTypes x:Key=" employeeTemplate"/>
Here VS2012 says: 'local:EmployeeTypes' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built.
Where i am wrong???
EmployeeTypes.cs
namespace SilverlightApplication1.Models
{
public class EmpolyeeTypes : List<EmployeeType>
{
public EmpolyeeTypes()
{
EmployeeType type;
type=new EmployeeType("Manager");
type.Employees.Add(new Employee("Michael"));
type.Employees.Add(new Employee("Paul"));
this.Add(type);
type = new EmployeeType("Project Managers");
type.Employees.Add(new Employee("Tim"));
type.Employees.Add(new Employee("John"));
type.Employees.Add(new Employee("David"));
this.Add(type);
}
}
}
<navigation:Page x:Class="SilverlightApplication1.Home"
xmlns:local="clr-namespace:SilverlightApplication1.Models"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480"
Title="Home"
Style="{StaticResource PageStyle}">
<local:EmployeeTypes x:Key=" employeeTemplate"/>
.......
.......
<Grid x:Name="LayoutRoot">
</Grid>
Try to move it to Resources of one UI control, for example as Resources of Grid :
<Grid x:Name="LayoutRoot">
<Grid.Resources>
<local:EmployeeTypes x:Key=" employeeTemplate"/>
</Grid.Resources>
</Grid>
or as Resources of the page :
<navigation:Page
......
>
<navigation:Page.Resources>
<local:EmployeeTypes x:Key=" employeeTemplate"/>
</navigation:Page.Resources>
</navigation:Page>
PS: my answer came from Windows Phone and WPF background, could be different in silverlight. Not sure.

Use a string as Window Height in XAML

I made a window with some parameters:
<Window x:Class="MsgBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MsgBox" Height="300" Width="500" Topmost="True" WindowStartupLocation="CenterScreen" WindowStyle="None" Loaded="MsgBox_Loaded">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"></ColumnDefinition>
<ColumnDefinition Width="2*"></ColumnDefinition>
</Grid.ColumnDefinitions>
</Grid>
</Window>
I want to change the height and width to these calculated strings. It gets the users screenwidth and height and divide it by four.
Public ReadOnly Property PrimaryScreenWidth As Double
Get
Return System.Windows.SystemParameters.PrimaryScreenWidth
End Get
End Property
Public ReadOnly Property PrimaryScreenHeight As Double
Get
Return System.Windows.SystemParameters.PrimaryScreenHeight
End Get
End Property
Private MsgBoxWidth As String = PrimaryScreenWidth \ 4
Private MsgBoxHeight As String = PrimaryScreenHeight \ 4
How to set it to my window?
Height="{x:static MsgBoxHeight }" Width="{x:static MsgBoxWidth }" ??
If you want to do this, why don't you simply
Me.Height = MsgBoxHeight
Me.Width = MsgBoxWidth
when you calculated the properties?
The syntax you're showing and presumably want, with the curly braces:
Height="{x:static MsgBoxHeight }" Width="{x:static MsgBoxWidth }"
is called Markup Extension syntax, it allows using a markup extension class to set a property value. Here's how you'd do it. First step, create the markup extenssion class:
Public Class MsgBoxHeight
Inherits System.Windows.Markup.MarkupExtension
Public Sub New()
End Sub
Public Overrides Function ProvideValue(serviceProvider As IServiceProvider) As Object
Return System.Windows.SystemParameters.PrimaryScreenHeight / 4
End Function
End Class
Next you'll add a xmlns:local="clr-namespace=YourNamespace" to the <Windows>, then you'll be able to use it like this: Height="{local:MsgBoxHeight}".
The complete XAML for the Window would look like this:
<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:WpfApplication6"
Title="MainWindow" Height="{local:MsgBoxHeight}" Width="525">
<Grid>
</Grid>
</Window>
Note
Using the Markup Extension means you're doing it from code. You are introducing a nice application-specific extension to XAML, but you need code for it to work. If you only need to do this for one Window it makes more sense to simply set the Height and Width of the window from the code-behind and not bother with the Markup Extension.

How to add UserControl to a Panel on a WPF Window

I think I'm missing something that should be obvious here, but I'm drawing a blank on this one.
I've built a very primitive UserControl containing nothing more than a TextBox to use as a log window:
<UserControl x:Class="My.LoggerControl"
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"
x:Name="LoggerView">
<Grid x:Name="LayoutRoot">
<TextBox x:Name="LogWindow" AcceptsReturn="True"/>
</Grid>
</UserControl>
I don't expect that to be the best way to do it, but it should be good enough for a prototype.
The code-behind is similarly simple:
public partial class LoggerControl : UserControl, ILogger
{
public LoggerControl()
{
InitializeComponent();
}
private LogLevel level = LogLevel.Warning;
#region ILogger
public LogLevel Level
{
get { return level; }
set { level = value; }
}
public void OnError(string s)
{
if (level >= LogLevel.Error)
LogWindow.AppendText("ERROR:::" + s + "\n");
}
// ...
#endregion
}
The thing I can't figure out is how to add this control to my MainWindow.xaml. Simplifying, lets say my window looks like this:
<Window x:Class="My.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:My"
Title="Test" Height="350" Width="525">
<Grid>
<local:LoggerControl x:Name="LogView" />
</Grid>
</Window>
Even with something so simple, the Designer in Visual Studio 2010 can't load the main window. The error given is:
A value of type 'LoggerControl' cannot be added to a collectionor dictionary of type 'UIElementCollection'.
This error message has only one unrelated hit in the major search engines (plus duplicates) so I haven't found any useful help. Microsoft's own documentation seems to imply that this should work.
Any idea how to solve this?
<UserControl x:Class="My.LoggerControl"
xmlns:local="clr-namespace:My.LogTest"
Looks like you may have made a mistake in the namespacing? LoggerControl is listed as being the namespace My, while you're importing My.LogTest and assigning it to the xml-prefix local. Change this to:
xmlns:local="clr-namespace:My"
And I think it should work. Otherwise, fix the LoggerControl declaration.

Resources