Reputation: 3153
I am designing a UI Library and I want to add two C# file
to a XAML
file so that in one file I can define only event Handler to be used in that Xaml file and in another file i can define some other UI related Style for controls. And i want my xaml
file to have access to my both C#
file class directly.
I am doing something like this using x:Class
:
For example:
I want to add a MainWindow.cs
file to my XAML file:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="TemplateLibrary.MainWindow">
But unfortunately XAML supports for only one class to get added to my XAML. Is there some way to add multiple C# file to a XAML file.
Upvotes: 2
Views: 4712
Reputation: 1
More basic is "How to bind an XAML visual object to code". Ranny Meier's suggestion on declaring my class on it's page as a 'partial class' coupled with (can't find him again) suggestion of using a redirection on the mainwindow page actually works fine - all in the same name space of course.
xaml
<Button x:Name="Pnl_Btn_EditPaste" Click="Do_Btn_EditPaste_Click" VerticalAlignment="Top" Padding="2" Margin="-67,18,0,0" ScrollViewer.VerticalScrollBarVisibility="Disabled" Width="36" Height="30" RenderTransformOrigin="1.976,-0.568">
<Button.OpacityMask>
<ImageBrush ImageSource="pack://siteoforigin:,,,/Resources/BnFace_paste.png"/>
</Button.OpacityMask>
<Button.Background>
<ImageBrush ImageSource="pack://siteoforigin:,,,/Resources/BnFace_paste.png"/>
</Button.Background>
</Button>
It's the "Click=" portion, or binding, on the Button opening statement we're interested in . . .
mainwindow c#
namespace WpfAppFoolAround
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
// -- Edit Controls ---------------------------------------------------------
private void Do_Btn_EditPaste_Click(object sender, RoutedEventArgs e)
{
ContextControls.Btn_EditPaste(sender, e);
}
}
}
The shim on mainwindow to redirect us to the class method . . .
the class on a different page
namespace WpfAppFoolAround
{
public partial class ContextControls
{
// -- Edit Controls ---------------------------------------------------
internal static void Btn_EditPaste(object sender, RoutedEventArgs e)
{
Console.WriteLine("PASTE Editing Button Clicked.");
}
}
}
And the actual method located in the class on the odd project page we added.
Upvotes: 0
Reputation: 75
It seems that the MainWindow code behind is already a partial class file, and therefore we could choose to place some members into additional cs files using the same partial class in the same namespace.
namespace TemplateLibrary
{
public partial class MainWindow : Window
{
...
The same applies when making a UserControl.
Upvotes: 1
Reputation: 22702
I think in your case you need to look in the direction of MVVM
, because the further complication of the project will cause some difficulties. You can create multiple ViewModel's
for a one View
. In the role of View
may suit:
UserControl
DataTemplate
In the case of UserControl
this class can be partial, which will complement the other class. In the case of DataTemplate
can dynamically change the content depending on View
type.
In addition, all styles of Control's must be contained in ResourceDictionary
and merged into the App.xaml
file.
Upvotes: 3