WPF:programmatically add RibbonBar from UserControl into a RibbonWindow RibbonTab - wpf

I have a WPF Ribbon based application and a DLL.
In the application I uses a third party source (Syncfusion) to create my RibbonWindow.
The DLL is a WPF class library that consist of a UserControl which is a RibbonBar.
I would like to add that UserControl to my RibbonWindow.
I'm not sure how to do that programmatically.
MainWindow.xaml.cs
Dynamically loaded the UserControl from the DLL
Assembly asm = Assembly.LoadFile( unitDllPath );
Type typ = asm.GetType( "WX" + ".UserControl1", true, true );
unitDll = Activator.CreateInstance( typ );
MainWindow.xaml
<syncfusion:RibbonTab Name="Tab1" IsChecked="True" >
</syncfusion:RibbonTab>
UserControl.xaml
<syncfusion:RibbonBar Header="Select" Name="Bar1" ></syncfusion:RibbonBar>
UserControl.xaml.cs
public void MainWindow()
{
InitializeComponent();
}
How can I get the RibbonBar from WX.UserControl (Bar1) into (Tab1)?
Thanks.

1)If we need to programmatically add UserControl which is a RibbonBar to your RibbonWindow, your UserControl should be like below
UserControl.xaml
<syncfusion:RibbonBar x:Class="WPFClassLibrary.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:syncfusion="http://schemas.syncfusion.com/wpf"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<syncfusion:RibbonButton SizeForm="Large" Label="New Email"/>
<syncfusion:RibbonButton SizeForm="Large" Label="Inbox"/>
<syncfusion:RibbonButton SizeForm="Large" Label="Drafts"/>
<syncfusion:RibbonButton SizeForm="Large" Label="OutBox"/>
2)Refer the assembly file in your application.
3)use that Assembly in code behind like below
using WPFClassLibrary;
4)now we can call that UserControl from code behind and add to the our RibbonWindow.
tab1.Items.Add(new UserControl1());
i have also attach simple sample for this
http://www.syncfusion.com/downloads/support/directtrac/general/ze/WPfRibbonSample-1343767783

Related

How can I set region Manager for my dialog window in Prism?

I write my app without using Shell. So I created my own Window using IDialogService and opened in one of my modules.
As far, as I am concerned, Region Manager is attached to Shell, but due to the fact I don't have it, region manager doesn't work when I try to navigate from one view to another.
I know that Region Navigation works fine with the shell (I tested it) and the same code stops working when I substitute the shell with IDialogService.
Here is what I have
<Window x:Class="TechDocs.Views.MainSettingsWindowView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True"
mc:Ignorable="d"
Title="MainSettingsWindow" Height="400" Width="750">
<Grid>
</Grid>
</Window>
Content for the first region. When I click the button, it should navigate to the second region.
<UserControl x:Class="TechDocs.Views.SettingsView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<Button Command="{Binding NodeSelectedCommand}" Name="Button"/>
<ContentControl prism:RegionManager.RegionName="region"/>
</Grid>
</UserControl>
In module I connect my root window with UserControl which holds the button and Content control for the second region.
public class SettingsModule : IModule
{
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
var dialogService = _containerProvider.Resolve<IDialogService>();
containerRegistry.RegisterDialog<MainSettingsWindow>("MyWindow");
containerRegistry.RegisterDialog<SettingsView>("customView");
containerRegistry.RegisterForNavigation<MyView>();
dialogService.Show("customView");
}
}
And when I click the button I get in this code
public void SelectedNode()
{
regionManager.RequestNavigate("region", "MyView");
}
RequestNavigate doesn't give any exceptions, but still nothing appears on the screen.
Could you please explain how I should register region manager with my window?
Try to initialize the region manager explicitly in your custom window using the static methods below:
RegionManager.SetRegionName(cc, "region");
RegionManager.SetRegionManager(cc, regionManager);
XAML:
<ContentControl x:Name="cc" prism:RegionManager.RegionName="region"/>

C# WPF XAML Mouse(Down) events not working anymore as of .NET 5(C#9)

Screen shot of the issue I'm running into after going to .NET 5 (C# 9)
I'm trying to figure out how they want us to implement this simple code now.
It says its an error but it still builds the project. Although, this does prevents designer from building the view from xaml, so unable to edit the xaml properly, also prevents the functionality, in this case trying to drag a window with mouse_down event.
XAML:
<Window x:Class="WPFProject.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WPFProject"
mc:Ignorable="d"
Title="Window1" Height="450" Width="800">
<Border MouseDown="CodeBehind_DragFunction">
<TextBlock Text="Hello World" Foreground="Black"/>
</Border>
</Window>
C# Code-behind
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
void CodeBehind_DragFunction(object sender_, MouseButtonEventArgs e_)
{
DragMove();
}
}
This worked in previous versions..
Suggested answer posted by #Pharaz Fadaei
It was an IDE bug that was fixed by following these steps outlined in this post: The event 'foo' is not a RoutedEvent

Can MahApps.Metro be used in MvvmCross?

I was trying to use Mahapps Metro inside a WPF project using MvvmCross mvvm framework.
Both seem to use custom window control. Is there a way to use both in a project?
Metro window:
<mah:MetroWindow x:Class="TipCalc.WPF.MainWindow"
xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TipCalc.WPF"
xmlns:views="clr-namespace:MvvmCross.Platforms.Wpf.Views;assembly=MvvmCross.Platforms.Wpf"
mc:Ignorable="d"
Title="MainWindow"
Height="450"
Width="800">
<Grid>
</Grid>
</mah:MetroWindow>
MvvmCross Window:
<views:MvxWindow x:Class="TipCalc.WPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TipCalc.WPF"
xmlns:views="clr-namespace:MvvmCross.Platforms.Wpf.Views;assembly=MvvmCross.Platforms.Wpf"
mc:Ignorable="d"
Title="MainWindow"
Height="450"
Width="800">
<Grid>
</Grid>
</views:MvxWindow>
The application runs either way. But with the Metro window in place, the rest of the application doesn't get set up(ie.-children views). With the MvvmCross window in place, the application works as usual but is not designed or colored.
Since multi inheritance is not supported in C#, this is not possible out of the box.
But a look at the source code of MvxWindow shows it's no big class. So a potential solution could be creation of your own window. This window, let's call it MvxMetroWindow, could collect both functionalities by inheriting from MetroWindow and aditionally all the source code of MvxWindow added (copied from the original source code) by yourself.
This would look like this:
using System;
using System.Windows;
using MahApps.Metro.Controls;
using MvvmCross;
using MvvmCross.Binding.BindingContext;
using MvvmCross.Platforms.Wpf.Views;
using MvvmCross.ViewModels;
using MvxApplication = MvvmCross.Platforms.Wpf.Views.MvxApplication;
namespace TipCalc.WPF
{
public class MvxMetroWindow : MetroWindow, IMvxWindow, IMvxWpfView, IDisposable
{
private IMvxViewModel _viewModel;
private IMvxBindingContext _bindingContext;
private bool _unloaded = false;
//... Further implemenetation of original MvxWindow
}
}
The custom window finally can be used like this:
<local:MvxMetroWindow x:Class="TipCalc.WPF.MainWindow"
xmlns:local="clr-namespace:TipCalc.WPF"
...

XAML Namespace Beginning with a Number

I have inheritted Button and am trying to add it to my main window.
It doesn't show up in the Toolbox. I have tried rebuilding several times.
I have tried adding xmlns:dc="clr-namespace:123Letters and xmlns:dc="clr-namespace:123Letters;assembly=123Letters to my MainWindow.
My MainWindow is:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="MainWin"
Title="123 Letters">
</Window>
My WeatherStationButton is:
<Button x:Class="WeatherStationButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
</Grid>
</Button>
and
Public Class WeatherStationButton
Inherits Button
Public Property WeatherStation As tblWeather
End Class
This is super simple stuff. I believe it's because XAML doesn't allow numbers in the first part of namespaces, but I can not find any reference to that anywhere, so I am asking if I am doing something wrong or is this one of XAML's "features"?
Just realized I could add my answer.
If your project name is 123Letters, you need to add an underscore before the first number when referencing it in XAML like, _123Letters.
Your clr-namespace declaration would then be:
xmlns:dc="clr-namespace:_123Letters"
and you could add your inherited button to your MainWindow like this:
<dc:WeatherStationButton x:Name="btnWS" />

User Control Test Container: Assembly doesn't contain any UserControls

This is killing me. I'm trying to test/debug a WPF UserControl library in Visual Studio 2010 with the User Control Test Container.exe. I keep getting the error "Assembly [...] doesn't contain any UserControls." This should be simple, and every msdn article and blog tutorial I've looked at suggests that it should all just work perfectly and magically.
Here's the code for a very simple test case which I cannot make work:
XAML:
<UserControl x:Class="UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<Label Content="Hello!" Height="28" HorizontalAlignment="Left" Margin="86,106,0,0" Name="Label1" VerticalAlignment="Top" />
</Grid>
Code Behind:
Public Class UserControl1
Inherits UserControl
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
End Sub
End Class
Project File has debug options set to open UserControlTestContainer.exe, with ./MyTestLibrary.dll as a command-line arg.
Any suggestions on how to make this work?
Just to be sure, your User control is being hosted in a Window or something right? I am pretty certain you cannot display a usercontrol all by itself. What does your containing window look like?
The UserControl Test Container is only for libraries build using the Windows Control Library Project Template. Even then, it's a subset since Visual C++ controls can't be tested using it.

Resources