I'm trying to build a word Add-in using VSTO, my add-in has a Task pane with WPF user control, is it possible to apply material design styling to the user control knowing that there is no app.xml in the VSTO project?
here is the user control XML
<UserControl x:Class="Test.MainTaskPane"
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:local="clr-namespace:TestUserControl"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<StackPanel Orientation="Horizontal" Height="80" HorizontalAlignment="Center" >
<TextBlock Text=" test the task pane" VerticalAlignment="Center"/>
<Button x:Name="btnUpdate" Content=" click me " Click="btnUpdate_Click"/>
</StackPanel>
and I'm calling this user control using ElementHost
private UserControl _MainTaskPane;
private Microsoft.Office.Tools.CustomTaskPane _DataTaskPane;
private bool _isTaskPaneVisibile=false;
public void setupTaskPane()
{
_MainTaskPane = new UserControl();
ElementHost _eh = new ElementHost { Child = new MainTaskPane() };
_MainTaskPane.Controls.Add(_eh);
_eh.Dock = DockStyle.Fill;
if (!_isTaskPaneVisibile)
{
_DataTaskPane = CustomTaskPanes.Add(_MainTaskPane, "Test task pane");
_DataTaskPane.DockPosition = Office.MsoCTPDockPosition.msoCTPDockPositionRight;
_DataTaskPane.Visible = true;
_DataTaskPane.Width = 300;
_isTaskPaneVisibile = true;
}
else
{
_DataTaskPane.Visible = !_DataTaskPane.Visible;
_DataTaskPane.Width = 300;
}
}
Related
Question: In the btnRevert_Click(...) event of the following code, how can we make the RichTextBox editable?
In my WPF .NET5 app, the btnTest_Click(...) event successfully displays the contents of a RichTextBox into a FlowDocumentReader (fdReader). Then the btnRevert_Click(...) event successfully takes the content back from fdReader and displays it back into RichTextBox, but it makes the RichTextBox ReadOnly.
I tested the scenario by entering simple text "This is a test" inside RichTextBox, and noted that the above back and forth scenario works fine except that the btnRevert_Click(...) event makes the text "This is a test" ReadOnly.
MainWindow.xaml:
<Window x:Class="Wpf_RTBFlowDocTest.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:Wpf_RTBFlowDocTest"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<DockPanel Name="mainPanel">
<ToolBar Name="mainToolBar" Height="30" DockPanel.Dock="Top">
<Button x:Name="btnTest" Content="Test" Click="btnTest_Click"/>
<Button x:Name="btnRevert" Content="Revert" Click="btnRevert_Click"/>
</ToolBar>
<RichTextBox Name="rtbTest" AcceptsTab="True"></RichTextBox>
<FlowDocumentReader x:Name="fdReader" Visibility="Collapsed">
<FlowDocument>
<Paragraph>Text inside paragraph.</Paragraph>
</FlowDocument>
</FlowDocumentReader>
</DockPanel>
</Grid>
</Window>
MainWindow.xaml.cs:
private void btnTest_Click(object sender, RoutedEventArgs e)
{
FlowDocument RTBflowDoc = rtbTest.Document;
rtbTest.Visibility = Visibility.Collapsed;
rtbTest.Document = new FlowDocument();
fdReader.Document = RTBflowDoc;
fdReader.Visibility = Visibility.Visible;
}
private void btnRevert_Click(object sender, RoutedEventArgs e)
{
FlowDocument fdReaderFlowDoc = fdReader.Document;
fdReader.Document = new FlowDocument();
rtbTest.IsReadOnly = false;
rtbTest.Document = fdReaderFlowDoc;
rtbTest.Visibility = Visibility.Visible;
}
The FlowDocumentReader provides a control to view a flow content (doesn't support edit).
The following lines in btnRevert_Click() method, from question above, just set rtbTest.Document to fdReader.Document:
FlowDocument fdReaderFlowDoc = fdReader.Document;
fdReader.Document = new FlowDocument();
rtbTest.Document = fdReaderFlowDoc;
But because of the FlowDocument in the FlowDocumentReader control doesn't support editing, the easiest way to obtain document from the FlowDocumentReader control for editing is to clone it like below:
private void btnRevert_Click(object sender, RoutedEventArgs e)
{
var range = new TextRange(fdReader.Document.ContentStart, fdReader.Document.ContentEnd);
if (!range.IsEmpty)
{
using (var stream = new MemoryStream())
{
range.Save(stream, DataFormats.XamlPackage);
var copyto = new TextRange(rtbTest.Document.ContentEnd, rtbTest.Document.ContentEnd);
copyto.Load(stream, DataFormats.XamlPackage);
}
}
rtbTest.Visibility = Visibility.Visible;
fdReader.Visibility = Visibility.Collapsed;
}
I'm using Bing map WPF control SDK to try retrieving coordinates and printing them
I've managed to retrieve the coordinates of the center of the current LocationRect using an EventHandler associated with pressing the arrows
I've tried employing the same concept with mouse clicking event handlers but it didn't work, first I've registered the event using the += notation as follows:
public MainWindow()
{
InitializeComponent();
MainMap.Mode = new AerialMode(true);
MainMap.Focus();
MainMap.Culture = "ar-sa";
MainMap.MouseDoubleClick += new MouseButtonEventHandler(MapWithPushpins_MouseDoubleClick);
}
private void MapWithPushpins_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
e.Handled = true;
Point mousePosition = e.GetPosition(this);
Location pinLocation = MainMap.ViewportPointToLocation(mousePosition);
Pushpin pin = new Pushpin();
pin.Location = pinLocation;
Coordinates.Text = pinLocation.Longitude.ToString();
MainMap.Children.Add(pin);
}
And here's the XAML file:
<Window x:Class="Ornina.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:m="clr-namespace:Microsoft.Maps.MapControl.WPF;assembly=Microsoft.Maps.MapControl.WPF"
xmlns:local="clr-namespace:Ornina"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid VerticalAlignment="Top" >
<m:Map CredentialsProvider="AqCitpgSjIz_Sxd6AyI9Zm1rs1uRSG_G3Y7ebfok69ufB8W8uRdUtvheaRbz_10t" x:Name="MainMap" Center="36,38" ZoomLevel="16" Mode="AerialWithLabels" HorizontalAlignment="Center" VerticalAlignment="Top" Height="300" Width="500">
</m:Map>
</Grid>
<TextBlock x:Name="Coordinates">Coordinations</TextBlock>
</Grid>
</Window>
The program isn't responding with any thing, no exceptions, no errors
Your TextBlock is in front of your map, so the map doesn't receive the MouseDoubleClick event.
You can change the TextBlock to:
<TextBlock x:Name="Coordinates"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Text="Coordinations" />
So it's only in the top left corner and not in front of the whole map.
Or you can move it outside of the map entirely, in a different grid row or column.
I have a question about finding resources in WPF, and I read the mechanism of finding resources from book.
It said that a UIElement will find its resources attribute first, if nothing fit then it will find its parent's resources attribute and so on, till application resources attribute.
I think it just like bubble routed event.
So I define a resource in stackpanel, here is the xaml.
<Window x:Class="DynamicResource.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:DynamicResource"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<StackPanel x:Name="stackPanel">
<StackPanel.Resources>
<TextBlock x:Key="res1" Text="this is staticresource" Background="Beige"></TextBlock>
<TextBlock x:Key="res2" Text="this is dynamicresource" Background="DarkGoldenrod"></TextBlock>
</StackPanel.Resources>
<Button Margin="5,5,5,0" Content="{StaticResource res1}"></Button>
<Button Margin="5,5,5,0" Content="{DynamicResource res2}"></Button>
<Button x:Name="button" Margin="5,5,5,0" Content="refresh resource" Click="Button_Click"></Button>
</StackPanel>
Then I try to change dynamic resource in the button click event, here is xaml.cs
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
//Can find target resource
//stackPanel.Resources["res1"] = new TextBlock() { Text = "this is new staticresource", Background = new SolidColorBrush(Colors.Tan) };
//stackPanel.Resources["res2"] = new TextBlock() { Text = "this it new dynamicresouce", Background = new SolidColorBrush(Colors.Magenta) };
//Can't find target resource
button.Resources["res1"] = new TextBlock() { Text = "this is new staticresource", Background = new SolidColorBrush(Colors.Tan) };
button.Resources["res2"] = new TextBlock() { Text = "this it new dynamicresouce", Background = new SolidColorBrush(Colors.Magenta) };
}
}
But the resource of button didn't change.
So what cause this happened, and how could to bubbly find resources.
you must set name for button and use this name in code behind
in xaml:
<Button Margin="5,5,5,0" Name="btnRes2" Content="{DynamicResource res2}"></Button>
in code behind :
private void Button_Click(object sender, RoutedEventArgs e)
{
btnRes2.Resources["res2"] = new TextBlock() { Text = "this it new dynamicresouce", Background = new SolidColorBrush(Colors.Magenta) };
}
this code work for DynamicResource.
I've recently had a go at embedding a PowerPoint file as an XpsDocument in WPF.
It is a simple WPF application in which I embed a DocumentViewer property into my MainWindow.xaml grid:
<Window x:Class="PowerPoint2.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:PowerPoint2"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<DocumentViewer
Name="DocumentviewPowerPoint"
VerticalAlignment="Top"
HorizontalAlignment="Left" />
</Grid>
To create the document bound to "DocumentviewPowerPoint" I convert the PowerPoint file that has been opened into Xps format and bind this variable to the XAML property mentioned earlier:
using System;
using System.IO;
using System.Windows;
using System.Windows.Xps.Packaging;
using Microsoft.Office.Core;
using Microsoft.Office.Interop.PowerPoint;
using Application = Microsoft.Office.Interop.PowerPoint.Application;
namespace PowerPoint2
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
const string powerPointFile = #"c:\temp\ppt.pptx";
var xpsFile = Path.GetTempPath() + Guid.NewGuid() + ".pptx";
var xpsDocument = ConvertPowerPointToXps(powerPointFile, xpsFile);
DocumentviewPowerPoint.Document = xpsDocument.GetFixedDocumentSequence();
}
private static XpsDocument ConvertPowerPointToXps(string pptFilename, string xpsFilename)
{
var pptApp = new Application();
var presentation = pptApp.Presentations.Open(pptFilename, MsoTriState.msoTrue, MsoTriState.msoFalse,
MsoTriState.msoFalse);
try
{
presentation.ExportAsFixedFormat(xpsFilename, PpFixedFormatType.ppFixedFormatTypeXPS);
}
catch (Exception ex)
{
MessageBox.Show("Failed to export to XPS format: " + ex);
}
finally
{
presentation.Close();
pptApp.Quit();
}
return new XpsDocument(xpsFilename, FileAccess.Read);
}
}
}
This all works well enough when running the program, showing the Xps document embedded into the WPF:
My question is how can I further modify my code in order to display the PowerPoint not just as a series of scrollable slides as shown, but as an actual slide show? I would like to make further updates to enable the user to navigate to the following slide on each mouse click - like a 'proper' presentation. My problem is that I am unfamiliar with the usage of the XpsDocument Apis - I don't know if it's a case of using these to achieve what I want or is it in the settings properties of the presentation variable that gets converted to the Xps format.
I managed to solve the particular problem I was interested in.
Please refer to this blog posting for a detailed explanation:
Controlling DocumentViewer methods and properties using MVVM
The solution addresses the problem of being able to enable individual PowerPoint slides (converted to xps file format) to occupy the WHOLE of the available windows space by invoking the relevant combination of DocumentViewer methods.
On pressing the screen button to invoke the RelayCommand, the following combination of DocumentViewer API calls in the MainWindowViewModel.cs class was observed to work:
public ICommand Command
{
get
{
return _command ?? (_command = new RelayCommand(
x =>
{
DocumentViewer = MainWindow.GetInstance();
const string powerPointFile = #"c:\temp\ppt.pptx";
var xpsFile = Path.GetTempPath() + Guid.NewGuid() + ".xps";
var xpsDocument = ConvertPowerPointToXps(powerPointFile, xpsFile);
FixedFixedDocumentSequence = xpsDocument.GetFixedDocumentSequence();
DocumentViewer.Document = FixedFixedDocumentSequence;
DocumentViewer.GoToPage(1);
DocumentViewer.FitToMaxPagesAcross(1);
WindowState = WindowState.Maximized;
DocumentViewer.FitToMaxPagesAcross(1);
}));
}
}
And to obtain the DocumentViewer instance itself? I also need to update the MainWindow.xaml.cs to get it to return the instance of the DocumentViewer object:
using System.Windows.Controls;
namespace DocumentView
{
public partial class MainWindow
{
private static DocumentViewer _docViewer;
public MainWindow()
{
InitializeComponent();
_docViewer = DocumentViewPowerPoint;
}
public static DocumentViewer GetInstance()
{
return _docViewer;
}
}
}
Where DocumentViewPowerPoint is the name given to the DocumentViewer in the MainWindow.xaml:
<Window x:Class="DocumentView.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:DocumentView"
mc:Ignorable="d"
WindowState="{Binding WindowState, Mode=TwoWay}"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<local:MainWindowViewModel />
</Window.DataContext>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="50" />
</Grid.RowDefinitions>
<DocumentViewer
Grid.Row="0"
Document="{Binding FixedFixedDocumentSequence}"
Name="DocumentViewPowerPoint"
VerticalAlignment="Top"
HorizontalAlignment="Left" />
<Button
Grid.Row="1"
Command="{Binding Command}"
Width="70" Height="30" Content="Press" />
</Grid>
I am trying to load a RenderWindowControl from vtk libraries on my WPF project using ActiViz.NET and Visual Studio 2013. The library works fine since I did a new project just to practice on it but when I try to integrate it into my work, the actor is not shown. This is my code:
MainWindow.xaml:
<Window x:Class="myProject.Views.MainWindow"
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:VtkTab="clr-namespace:myProject.Views.UITabs.VtkTab"
x:Name="Mainwindow"
MinHeight="600"
MinWidth="800"
Title="{Binding Title}"
Height="720"
Width="1280"
Icon="{StaticResource ApplicationIcon}"
Loaded="OnLoaded"
DataContext="{Binding Main, Source={StaticResource ViewModelLocator}}"
Style="{StaticResource WindowStyle}"
mc:Ignorable="d">
<DockPanel>
<TabControl>
....
....
<VtkTab:VtkTabView />
....
....
</TabControl>
</DockPanel>
</Window>
VtkTabView.xaml:
<UserControl x:Class="myProject.Views.UITabs.VtkTab.VtkTabView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vtk="clr-namespace:Kitware.VTK;assembly=Kitware.VTK"
Height="480" Width="640">
<WindowsFormsHost Name="Wfh">
<vtk:RenderWindowControl x:Name="RenderControl" />
</WindowsFormsHost>
</UserControl>
VtkTabView.xaml.cs:
public partial class UITabView
{
protected static Random _random = new Random();
vtkActor actor = vtkActor.New();
public VtkTabView()
{
InitializeComponent();
var sphere = vtkSphereSource.New();
sphere.SetThetaResolution(8);
sphere.SetPhiResolution(16);
var shrink = vtkShrinkPolyData.New();
shrink.SetInputConnection(sphere.GetOutputPort());
shrink.SetShrinkFactor(0.9);
var move = vtkTransform.New();
move.Translate(_random.NextDouble(), _random.NextDouble(), _random.NextDouble());
var moveFilter = vtkTransformPolyDataFilter.New();
moveFilter.SetTransform(move);
moveFilter.SetInputConnection(shrink.GetOutputPort());
var mapper = vtkPolyDataMapper.New();
mapper.SetInputConnection(moveFilter.GetOutputPort());
// The actor links the data pipeline to the rendering subsystem
actor.SetMapper(mapper);
actor.GetProperty().SetColor(1, 0, 0);
RenderControl.Load += RenderWindowControlOnLoad;
}
void RenderWindowControlOnLoad(object sender, EventArgs e)
{
var renderer = RenderControl.RenderWindow.GetRenderers().GetFirstRenderer();
renderer.AddActor(actor);
renderer.SetBackground(0, 1, 0);
renderer.Render();
}
}
As you can see, in the method RenderWindowControlOnLoad (VtkTabView.xaml.cs) I take the actor (a Sphere) and I add it to the render declared into the WPF but it is not displayed. I tried to change the background color and call to render just in case but it's still not working :(
Call
RenderControl.RenderWindow.Render();
instead of
renderer.Render();