[英]Modify Resource in Design Time, with code behind in WPF project
What I'm trying to do right now: Modify the Expression Blend UI / Visual Studio, to add a button on one of my dependency properties, and when I click on it, it creates a new trigger. 我现在想做的是:修改Expression Blend UI / Visual Studio,在我的一个依赖项属性上添加一个按钮,当我单击它时,它会创建一个新触发器。
What is working : I created the button, it appears in the UI, that's fine. 正常工作 :我创建了按钮,它出现在UI中,很好。
What is not working : I cannot modify the Resource to add a trigger (if I step in, it works but it does not modify on the global resource, only on the instance I think). 什么不起作用 :我无法修改资源以添加触发器(如果我介入,它可以工作,但不会在全局资源上(仅在我认为的实例上)进行修改)。
I have my main project in Visual Studio, and a property with a button like this: 我在Visual Studio中有我的主项目,还有一个带有如下按钮的属性:
When the button is pressed this is what happens: 当按下按钮时,将发生以下情况:
I think that it's because I only modify it on memory, so it's in the "air" 我认为这是因为我仅在内存上对其进行了修改,所以它处于“空中”状态
I don't know where to go now... I need help 我现在不知道要去哪里...我需要帮助
The code behind where I modify the Resource is in an other DLL, the MyLibrary. 我修改资源所在的代码位于另一个DLL MyLibrary中。 Design .cs
设计 .cs
I'm using Visual Studio 2010 / Blend 4 / .NET 4.0 我正在使用Visual Studio 2010 / Blend 4 / .NET 4.0
That's one splution I can figure at the moment, but unfortunately I'm not able to test it now. 目前,这是我能想到的一种情况,但是很遗憾,我现在无法对其进行测试。 Still you can check if it works for you.
您仍然可以检查它是否适合您。
You can have a class, say ResourceContainer.cs
, and collect your control in it as a public static value, and let your control be a button: 您可以拥有一个类,例如
ResourceContainer.cs
,并将其中的控件收集为一个公共静态值,然后让您的控件成为一个按钮:
public static Button MyButton;
Then you can use it in your code-behind: If your window is MainWindow.xaml
, and, say you need your particular control in a grid, then you probably have something like: 然后,可以在后台代码中使用它:如果您的窗口是
MainWindow.xaml
,并且说您需要在网格中使用特定的控件,则可能会有类似以下内容:
<Window x:Class=...
...(namespace stuff)...
Title="MainWindow" ...(size stuff)...>
<Grid x:Name="MyGrid">
...(your code here)...
</Grid>
...
</Window>
Then in MainWindow.xaml.cs
you can use ResourceContainer.cs
like this: 然后,可以在
MainWindow.xaml.cs
中使用ResourceContainer.cs
如下所示:
...
ResourceContainer rc;
...
MyGrid.Resources.Add("MyKey", rc.MyButton);
...
(when you need it)
rc.MyButton.Triggers.Add(TriggerBase item);
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.