简体   繁体   English

将XAML插入编辑器中的WPF自定义控件

[英]Insert XAML into WPF custom control in the editor

I have a XAML main window that contains a header, a central area and a footer (in a grid). 我有一个XAML主窗口,其中包含一个页眉,一个中心区域和一个页脚(在网格中)。 The central area contains a ContentControl which is set throw a binding (using MVVMLight). 中央区域包含一个ContentControl,它设置为抛出绑定(使用MVVMLight)。 The header/footer is always the same so no problems there. 页眉/页脚始终相同,因此没有问题。

The part that goes into the ContentControl is always quite similar, they are WPF usercontrols and have a left part that contains info and a right part with at least an OK and BACK button. 进入ContentControl部分总是非常相似,它们是WPF用户控件,左侧部分包含信息,右侧部分至少具有一个OK和BACK按钮。

These are viewmodels and their views: 这些是视图模型及其视图:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="2*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Grid Grid.Column="0">
        <TextBlock Text="this changes and contains other controls too" />            
    </Grid>
    <Grid Grid.Column="1">
        <!-- more buttons and statuses-->
        <Button Content="Back" Margin="5" Height="30" />
        <Button Content="Ok" Margin="5" Height="30" />
    </Grid>
</Grid>

Is there a way i could create a base class/custom control for those views? 有没有办法为这些视图创建基类/自定义控件? So that I could write something like this in my xaml: 这样我就可以在xaml中写这样的东西:

<basewindow>
    <leftpart>
        custom XAML for this view
    </leftpart>
    <rightpart>
        custom XAML for this view
    </rightpart>
</basewindow>

I could then remove duplicate code that is now in each of those views to the base class while still keeping the ability to write my xaml in the editor. 然后,我可以将这些视图中每个视图中的重复代码删除到基类中,同时仍然保持在编辑器中编写xaml的能力。 Or is this not feasible? 还是不可行?

To clarify are you trying to inherit the visual element that exist in XAML, like you can do in WinForms? 为了澄清您是否要像在WinForms中那样继承XAML中存在的可视元素? If so you cannot do this in WPF. 如果是这样,您将无法在WPF中执行此操作。 There is no Visual inheritence in WPF. WPF中没有视觉继承。

Now if you aren't trying to inherit visual element it is easy. 现在,如果您不想继承视觉元素,这很容易。 First create your UserControlBase class and add you event handler. 首先创建您的UserControlBase类,然后添加事件处理程序。 Keep in mind this base class can not have any XAML associated with it. 请记住,此基类不能与任何XAML相关联。 Code only 仅代码

public class MyUserControlBase : UserControl
{
    public MyUserControlBase()
    {

    }

    protected virtual void Button_Click(object sender, RoutedEventArgs e)
    {

    }
}

Now create another UserControl that does have a xaml counter part. 现在,创建另一个具有xaml计数器部分的UserControl。 Now you will need to change the root elemtn in the XAML to your base class like this: 现在,您需要像这样将XAML中的根elemtn更改为您的基类:

<local:MyUserControlBase x:Class="WpfApplication7.MyUserControl"
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   xmlns:local="clr-namespace:WpfApplication7">

<Grid>
   <Button Click="Button_Click">My Button</Button>
</Grid>
</local:MyUserControlBase>

And don't forget the code behind: 并且不要忘记后面的代码:

public partial class MyUserControl : MyUserControlBase
{
    public MyUserControl()
    {
        InitializeComponent();
    }
}

Notice the button in the derived user control is calling the Button_Click event handler we defined in the base class. 请注意,派生用户控件中的按钮正在调用我们在基类中定义的Button_Click事件处理程序。 That is all you need to do. 这就是您需要做的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM