wpf 如何使用 Visual Studio“合并”XAML 文件及其代码隐藏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18121494/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to 'merge' a XAML file and its code-behind with Visual Studio
提问by Morgan M.
I have a template defined in a XAML file named 'MyTemplate.xaml'. This template is using a code-behind file named 'MyTemplate.cs'.
我在名为“MyTemplate.xaml”的 XAML 文件中定义了一个模板。此模板使用名为“MyTemplate.cs”的代码隐藏文件。
Mytemplate.xaml:
我的模板.xaml:
<ResourceDictionary x:Class="Project.Templates.MyTemplate">
<DataTemplate ... />
</ResourceDictionary>
MyTemplate.cs:
我的模板.cs:
namespace Project.Templates
{
public partial class MyTemplate : ResourceDictionary
{
...
}
}
In the Visual Studio Solution Explorer, these two files are side by side. What I would like to do is to put these two files together, just like with a Control and its code-behind.
在 Visual Studio 解决方案资源管理器中,这两个文件是并排的。我想做的是将这两个文件放在一起,就像使用 Control 及其代码隐藏一样。
What I have: 
我拥有的: 
What I would like to have: 
我想要什么: 
What is the best way to do that? Thank you.
最好的方法是什么?谢谢你。
回答by Thomas Levesque
You need to edit the .csproj file. Find the <Compile>element for MyTemplate.cs, and add a <DependentUpon>element under it:
您需要编辑 .csproj 文件。找到<Compile>MyTemplate.cs的元素,并<DependentUpon>在其下添加一个元素:
<Compile Include="MyTemplate.cs">
<DependentUpon>MyTemplate.xaml</DependentUpon>
</Compile>
See this blog post: make a project item a child item of another
请参阅此博客文章:使项目项成为另一个项目的子项
回答by usefulBee
Another way is to:
另一种方法是:
- Add/Create a new XAML file/item
- Copy and paste the old .xaml and xaml.cs content to the new equivalent files
- delete the separate files
- rename the new file
- 添加/创建新的 XAML 文件/项目
- 将旧的 .xaml 和 xaml.cs 内容复制并粘贴到新的等效文件中
- 删除单独的文件
- 重命名新文件
回答by JMK
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 类,使用无参数构造函数将其设为公共和非静态。
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:
在 App.xaml 文件中将您的 ViewModel 添加为静态资源:
<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.
繁荣,没有代码隐藏。希望这可以帮助。
回答by Sheridan
If you really feel the need to combine pieces of code behind with XAML in WPF, you could using Attached Properties. You can store the XAML in App.Resourcesand load it and apply it from an Attached Property. I do realise that this will not give you one file for the XAML and code behind, but it does work nicely.
如果您真的觉得需要将背后的代码片段与 WPF 中的 XAML 结合起来,您可以使用Attached Properties. 您可以将 XAML 存储在其中App.Resources并加载它并从Attached Property. 我确实意识到这不会为 XAML 和背后的代码提供一个文件,但它确实工作得很好。
You can register a PropertyChangedCallbackhandler in your Attached Propertyand in this handler, you can apply the template:
您可以PropertyChangedCallback在您的Attached Property和此处理程序中注册一个处理程序,您可以应用模板:
ControlTemplate controlTemplate = (ControlTemplate)Application.Current.
FindResource("TextBoxTemplate");
if (textBox.Template != controlTemplate) textBox.Template = controlTemplate;

