We demonstrated creating a WPF Window from an External Command of Revit Addins before either through creating a WPF window on the fly and hosting a User Control into it or through converting a WPF Application project to a Revit Class Library then creating a WPF window from an XMAL template directly.
We also made the created-on-the-fly WPF window/control look good through tweaking many properties of both the host window and the hosted control and added some fancy effects to both the background of the window/control and a particular button.
A cool feature about WPF is that we can make the same user control/window and its buttons look different but all cool, or have different nice skins at different times. We can achieve the goal through applying a theme to the whole WPF user control or window.
We can apply themes in many ways. The themes can be from some external loose XMAL files or from some internal compiled/embedded theme XMAL resources either in the same assembly or in separate ones. And we can statically specify the theme for the WPF Control/Window in the XMAL files or dynamically merge the theme definitions into its resource dictionaries with code.
We are going to add some skin to the User Control that we have demonstrated before through loading a loose (external) theme XMAL through code and applying the theme to the control dynamically.
A theme XMAL needs to be collected and copied along with the WPF-compliant Revit Addin assembly. We use a sample theme provided by the WPF Toolkit, Download BureauBlue. In case it is not handy for readers, please click the link to get a copy.
NOTE: To organize the theme file and the project better, the XMAL file will also be added to the project, and its Build Action is not necessary other than None but the Copy to Output Directory has to be Copy if newer or Copy always. Otherwise, the code we are going to demonstrate will not find the theme XMAL external file since it will not be created programmatically by the assembly. We can certainly copy and paste the theme XMAL to the output folder manually, but that is not user friendly thus not what we really want.
No need to change anything to the XMAL definition file of the WPF User Control, either having a theme dictionary resource added as demonstrated previously or not. What we need to do is to add the highlighted code right after the WPF User Control creation and before setting it as the content of the WPF window so that the final Revit external command Execute code looks like:
public Result Execute(ExternalCommandData cmdData, ref string msg, ElementSet elemSet)
{
_cachedCmdData = cmdData;
try
{
//TODO: add your code below.
UserControl1 userControl = new UserControl1();
// Load a theme and set it as current.
ResourceDictionary theme = new ResourceDictionary();
//theme.Source = new Uri(@"BureauBlue.xaml", UriKind.Relative);
string themePath = System.IO.Path.Combine(
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
"BureauBlue.xaml");
theme.Source = new Uri(themePath, UriKind.Absolute);
userControl.Resources.MergedDictionaries.Clear();
userControl.Resources.MergedDictionaries.Add(theme);
Window win = new Window();
win.Content = userControl;
win.Width = win.Height = double.NaN;
win.SizeToContent = SizeToContent.WidthAndHeight;
win.ShowInTaskbar = win.Topmost = true;
win.ResizeMode = ResizeMode.NoResize;
win.WindowStartupLocation = WindowStartupLocation.CenterScreen;
win.HorizontalAlignment = win.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Left;
win.VerticalAlignment = win.VerticalContentAlignment = VerticalAlignment.Top;
System.Windows.MessageBox.Show(string.Format("OKed? : {0}", win.ShowDialog()));
return Result.Succeeded;
}
catch (Exception ex)
{
msg = ex.ToString();
return Result.Failed;
}
}
Now the project should build well and we are ready to give it a try immediately by pressing the F5 since Revit Addin Wizard (RevitAddinWizard) has already set up the debugging settings for us automatically.
After the ExtCmd ribbon button of the RevitAddinCSWPF ribbon panel is clicked, the WPF Window hosting the User Control shows up with the cool bureau blue skin this time:
A few check boxes and radio buttons have also been added to the WPF User Control to make the skin effects appear clearer and nicer.
More articles about WPF and Revit Addin/API can be expected soon. Please stay tuned.
RevitAddinWizard may provide a few coders to address some WPF usage cases in the near future.
Recent Comments