简体   繁体   English

在设计时修改资源,在WPF项目中添加代码

[英]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中有我的主项目,还有一个带有如下按钮的属性:

Visual Studio用户界面

When the button is pressed this is what happens: 当按下按钮时,将发生以下情况:

  • I get my Control that contains that dependency property (Ok). 我得到包含该依赖项属性(OK)的控件。
  • I searched for the Resource file that contains the Resource I want to modify (Ok). 我搜索了包含要修改的资源的资源文件(确定)。
  • I update the Resource, but it does not replace the Resource on disk. 我更新了资源,但它不会替换磁盘上的资源。

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.

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