简体   繁体   English

如何使用Visual Studio“合并”XAML文件及其代码隐藏

[英]How to 'merge' a XAML file and its code-behind with Visual Studio

I have a template defined in a XAML file named 'MyTemplate.xaml'. 我有一个名为'MyTemplate.xaml'的XAML文件中定义的模板。 This template is using a code-behind file named 'MyTemplate.cs'. 此模板使用名为“MyTemplate.cs”的代码隐藏文件。

Mytemplate.xaml : Mytemplate.xaml

<ResourceDictionary x:Class="Project.Templates.MyTemplate">
    <DataTemplate ... />
</ResourceDictionary>

MyTemplate.cs : MyTemplate.cs

namespace Project.Templates
{
    public partial class MyTemplate : ResourceDictionary
    {
        ...
    }
}

In the Visual Studio Solution Explorer, these two files are side by side. 在Visual Studio解决方案资源管理器中,这两个文件是并排的。 What I would like to do is to put these two files together, just like with a Control and its code-behind. 我想要做的是将这两个文件放在一起,就像使用Control及其代码隐藏一样。

What I have: 我有的: 在此输入图像描述

What I would like to have: 我想拥有什么: 在此输入图像描述

What is the best way to do that? 最好的方法是什么? Thank you. 谢谢。

You need to edit the .csproj file. 您需要编辑.csproj文件。 Find the <Compile> element for MyTemplate.cs, and add a <DependentUpon> element under it: 找到MyTemplate.cs的<Compile>元素,并在其下添加一个<DependentUpon>元素:

<Compile Include="MyTemplate.cs">
  <DependentUpon>MyTemplate.xaml</DependentUpon>
</Compile>

See this blog post: make a project item a child item of another 请参阅此博客文章: 将项目项目作为另一项目的子项目

Another way is to: 另一种方式是:

  • Add/Create a new XAML file/item 添加/创建新的XAML文件/项目
  • Copy and paste the old .xaml and xaml.cs content to the new equivalent files 将旧的.xaml和xaml.cs内容复制并粘贴到新的等效文件中
  • delete the separate files 删除单独的文件
  • rename the new file 重命名新文件

This isn't an answer to your initial question, but to this: 这不是您最初问题的答案,而是对此:

In that case, please explain how to add an event handler to a template without using code-behind 在这种情况下,请解释如何在不使用代码隐藏的情况下向模板添加事件处理程序

You can do this with a ViewModel and an ICommand class. 您可以使用ViewModel和ICommand类执行此操作。

First you need to create your ViewModel class, make this public and non static with a parameter-less constructor. 首先,您需要创建ViewModel类,使用无参数构造函数使其成为public和non static。

Then create another class which implements the ICommand interface: 然后创建另一个实现ICommand接口的类:

public class Command : ICommand
{
    public void Execute(object parameter)
    {
        //this is what happens when you respond to the event
    }

    public bool CanExecute(object parameter)
    {
        return true;
    }

    public event EventHandler CanExecuteChanged;
}

Add an instance of your command class to your ViewModel class, make this private and expose it through a read-only property: 将命令类的实例添加到ViewModel类,将其设为私有并通过只读属性公开:

public class ViewModel
{
    private readonly ICommand _command = new Command();

    public ICommand Command
    {
        get { return _command; }
    }
}

Add your ViewModel as a static resource, in your App.xaml file: 将ViewModel添加为App.xaml文件中的静态资源:

<Application.Resources>
     <wpfApplication1:ViewModel x:Key="ViewModel"/>
</Application.Resources>

Set your DataContext of your XAML file to your ViewModel: 将XAML文件的DataContext设置为ViewModel:

<Window DataContext="{StaticResource ViewModel}">

Now respond to your event by binding to the Command class: 现在通过绑定到Command类来响应您的事件:

<Button Click="{Binding Command}"></Button>

Boom, no code-behind. 热潮,没有代码抛弃。 Hope this helps. 希望这可以帮助。

If you really feel the need to combine pieces of code behind with XAML in WPF, you could using Attached Properties . 如果您真的觉得需要在WPF中将代码片段与XAML结合起来,则可以使用Attached Properties You can store the XAML in App.Resources and load it and apply it from an Attached Property . 您可以将XAML存储在App.Resources并加载它并从Attached Property应用它。 I do realise that this will not give you one file for the XAML and code behind, but it does work nicely. 我确实意识到这不会给你一个XAML和后面的代码文件,但它确实很好用。

You can register a PropertyChangedCallback handler in your Attached Property and in this handler, you can apply the template: 您可以在Attached Property注册PropertyChangedCallback处理程序,在此处理程序中,您可以应用模板:

ControlTemplate controlTemplate = (ControlTemplate)Application.Current.
    FindResource("TextBoxTemplate");
if (textBox.Template != controlTemplate) textBox.Template = controlTemplate;

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

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