I have usercontrols which inherit a base class which looks like this:
BasePage.cs:
using System.Windows.Controls;
namespace TestPageManager23434
{
public class BasePage : UserControl
{
public string ButtonPreviousText { get; set; }
public string ButtonNextText { get; set; }
protected PageManager pageManager;
public BasePage(PageManager pageManager)
{
this.pageManager = pageManager;
}
}
}
And the user controls which look like this:
XAML:
<local:BasePage x:Class="TestPageManager23434.Page1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:local="clr-namespace:TestPageManager23434"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel Background="White"
Width="400"
Height="400"
>
<TextBlock Text="this is page1"/>
<TextBlock Text="{Binding Message}"/>
<Button Content="go to page 2" Click="Button_Click"
HorizontalAlignment="Left"
Width="200"
Height="30"
/>
</StackPanel>
</local:BasePage>
Code Behind:
using System.Windows.Controls;
namespace TestPageManager23434
{
public partial class Page1 : BasePage
{
public string Message { get; set; }
public Page1(PageManager pageManager) : base(pageManager)
{
InitializeComponent();
DataContext = this;
}
private void Button_Click(object sender, System.Win开发者_如何学Godows.RoutedEventArgs e)
{
pageManager.SwitchPage("page2");
}
}
}
But now in my XAML portions of my various UserControls I also have XAML blocks which I would also like to inherit from the base class so that I don't have this block copied in each XAML file:
<StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Left">
<Button
Content="{Binding ButtonPreviousText}"
Width="150"
Margin="5"
Click="ButtonPrevious_Click"/>
<Button
Content="{Binding ButtonNextText}"
Width="150"
Click="ButtonNext_Click" Margin="5"/>
</StackPanel>
But since my base class has no XAML, I see two ways:
- create them in code in my base class
- create usercontrols for them
Or is there another way to have this XAML in one location or some way to inherit XAML?
I suggest reading the WPF Control Authoring Overview over on MSDN. I don't think you'll be able to achieve what you're trying to do with UserControls especially. UserControls are pretty much meant to glue a certain set of controls together in a way that they'll be reused consistently within the scope of a single application. They are really not meant to be inherited.
One thing you can do is use them in a sort of "MasterPage" fashion (if you're familiar with ASP.NET). You can put ContentControls inside the "base" UserControl and expose different properties which allow others to inject content into the standard layout of the base control. In this way your other UserControl's put the "base" UserControl as the root element inside of themselves and set the various content properties to whatever elements they want.
In your example it sounds you would be best served by creating a control called "NavigationControl" and then use that in each of your controls rather than copying this same block. Without a little more context and specifics its hard to give a good solution.
Well you could write the xaml and then use the XamlReader to parse the xaml to objects, it gives you the advantages of writing xml instead of code.
If your every control has such common items then you can make a user control of common items and use it inside every other user control.
Second, I can see that you have some business logic in the common item, however the events way of writing business logic is little older, you should consider creating commands and implement commands. This will require little more study but it will let you design bigger projects easily.
精彩评论