How Replace Xaml Text with Text based on MarkUp Extensions automatically - wpf

We have some translation mark up extension which looks like this:
TextBlock Text="{l:Translate 'My string'}"
and we want (because we could use some other tool for xaml translation) to replace text tags inside of all project xamls automatically.
Is there any way to find out all nodes or attributes with regex or with xml reader/write to implement this case?

Essentially, XAML meets the standard XML, but to work with it, you need external libraries. As an example: Microsoft XAML Toolkit CTP (download). Simple example, which displays a list of items:
// Previously adding the library
using Microsoft.Xaml.Tools.XamlDom;
XamlDomObject rootObject = XamlDomServices.Load("MainWindow.xaml");
foreach (XamlDomObject domObject in rootObject.DescendantsAndSelf())
{
MessageBox.Show(domObject.Type.ToString());
}
Set Background on every Control in your document:
XamlDomObject rootObject = XamlDomServices.Load("MainWindow.xaml");
foreach (XamlDomObject objectNode in
from control in rootObject.DescendantsAndSelf(typeof(Control))
where !control.HasMember("Background")
select control)
{
objectNode.SetMemberValue("Background", "Red");
}
XamlDomServices.Save(rootObject, "NewFile.xaml");
For replace value of Text property, I use example:
private void Window_ContentRendered(object sender, EventArgs e)
{
XamlDomObject rootObject = XamlDomServices.Load("MainWindow.xaml");
foreach (XamlDomObject objectNode in
from control in rootObject.DescendantsAndSelf(typeof(TextBlock))
where control.HasMember("Text")
select control)
{
objectNode.SetMemberValue("Text", "MyInsertedText");
}
XamlDomServices.Save(rootObject, "NewFile.xaml");
}
File Input:
<Window x:Class="XAMLdom.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" ContentRendered="Window_ContentRendered">
<Grid>
<TextBlock Text="SomeText" Width="100" Height="30" />
</Grid>
</Window>
File Output:
<?xml version="1.0" encoding="utf-8"?>
<Window xml:base="file:///C:/Documents and Settings/Kanc/мои документы/visual studio 2010/Projects/XAMLdom/XAMLdom/bin/Debug/MainWindow.xaml" x:Class="XAMLdom.MainWindow" Title="MainWindow" Height="350" Width="525" ContentRendered="Window_ContentRendered" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<TextBlock Text="MyInsertedText" Width="100" Height="30" />
</Grid>
</Window>

We have used regular expressions which is not the best way but we can leave with it.

Related

How to change color of WPF HelixViewport3D cube and write text on it

The following code:
<Window x:Class="helixCube.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:h="http://helix-toolkit.org/wpf"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:helixPerfectCube"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid Width="200" Height="200" Background="Red">
<h:HelixViewport3D Title="Titlee" ShowViewCube="False" TextBrush="Black" Opacity="0.9">
<h:SunLight/>
<h:BoxVisual3D Width="12" Length="12" Height="12"/>
</h:HelixViewport3D>
</Grid>
</Window>
gives me the following cube:
I would like to make the following 2 changes:
I want the color of the cube to be black. However, I can't find a way to do this.
I want the title to be displyed directly on (the surface of) the cube.
Does anybody know how this can be done?
In viewmodel, Create some materials as follows:
public Model3D Model {get; set;}
public MainViewModel()
{
var outsideMaterial = MaterialHelper.CreateMaterial(Colors.LightGray, 0.1);
var insideMaterial = MaterialHelper.CreateMaterial(Colors.Gray);
var modelGroup = new Model3DGroup();
modelGroup.Children.Add(new GeometryModel3D {Geometry = cubeObj, Material =outsideMaterial, BackMaterial = insideMaterial});
}
Create a cube object.! (cubeObj) After that
In Ui,
<h:HelixViewport3D Title="Titlee" ShowViewCube="False" TextBrush="Black" Opacity="0.9">
<h:SunLight/>
<ModelVisual3D Content = "{Binding Model}"/>
</h:HelixViewport3D>
Helix ViewPort allows rotation, pan and zoom. text doesn't render neatly in such 3d space while performing above actions. Hence, it isn't recommended to put text on top of the cube.
Disclaimer: Untested code.!

Call instance of class in Xaml

Help me understand.
I create template of SilverlightApplication1 in VS2012. Create in my solution folder Models and add class EmployeeTypes.cs. I need to call up the instance of EmployeeTypes.cs in Xaml file (Home.xaml).
I do so in Home.xaml:
<navigation:Page x:Class="SilverlightApplication1.Home"
xmlns:local="clr-namespace:SilverlightApplication1.Models"
....
....
Then i do so ( all this according tutorial http://weblogs.asp.net/psheriff/archive/2012/04/09/silverlight-tree-view-with-multiple-levels.aspx):
<local:EmployeeTypes x:Key=" employeeTemplate"/>
Here VS2012 says: 'local:EmployeeTypes' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built.
Where i am wrong???
EmployeeTypes.cs
namespace SilverlightApplication1.Models
{
public class EmpolyeeTypes : List<EmployeeType>
{
public EmpolyeeTypes()
{
EmployeeType type;
type=new EmployeeType("Manager");
type.Employees.Add(new Employee("Michael"));
type.Employees.Add(new Employee("Paul"));
this.Add(type);
type = new EmployeeType("Project Managers");
type.Employees.Add(new Employee("Tim"));
type.Employees.Add(new Employee("John"));
type.Employees.Add(new Employee("David"));
this.Add(type);
}
}
}
<navigation:Page x:Class="SilverlightApplication1.Home"
xmlns:local="clr-namespace:SilverlightApplication1.Models"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480"
Title="Home"
Style="{StaticResource PageStyle}">
<local:EmployeeTypes x:Key=" employeeTemplate"/>
.......
.......
<Grid x:Name="LayoutRoot">
</Grid>
Try to move it to Resources of one UI control, for example as Resources of Grid :
<Grid x:Name="LayoutRoot">
<Grid.Resources>
<local:EmployeeTypes x:Key=" employeeTemplate"/>
</Grid.Resources>
</Grid>
or as Resources of the page :
<navigation:Page
......
>
<navigation:Page.Resources>
<local:EmployeeTypes x:Key=" employeeTemplate"/>
</navigation:Page.Resources>
</navigation:Page>
PS: my answer came from Windows Phone and WPF background, could be different in silverlight. Not sure.

local wpf binding How should be done?

guys I wanna use my own "usercontrol" in the main window, but i have problem with this line <local:texttoolbar x:Name="toolbar" DockPanel.Dock="Top">
The name of project is wpfin8 so i put it in the line with namespace
xmlns:local="clr-namespace:wpfin8">
main user control xaml code looks like this
<UserControl x:Class="TextEditor.TextEditorToolbar"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Loaded="UserControl_Loaded">
and the name is texttoolbar.xaml
The piece of mainwindow code is
<Window x:Class="wpfin8.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
xmlns:local="clr-namespace:wpfin8">
<Menu x:Name="menu"
DockPanel.Dock="Top"></Menu>
<local:texttoolbar x:Name="toolbar" DockPanel.Dock="Top">
<ToolBar>
<Button >open
</Button>
<Button Margin="10,0,0,0">
save
</Button>
</ToolBar>
</local:texttoolbar>
I think i did everything properly and it dont want work and it still underlined with this "the name texttoolbar does not exist in the namespace "clr-namespace:wpfin8" " , I've also tried with the "TextEditorToolbar" , "TextEditor"
U Need to add assembly name also to your UserControl namespace used in MainWindow
like->
xmlns:local="clr-namespace:wpfin8;assembly=wpfin8"
assembly name may be different .. U can check that in your UserControl Properties named under AssemblyNamespace..
Hope it helps..:)

How to Use RadWindow as MainWindow of WPF application

I want to use RadWindow as the main window of my WPF application. So that outer window also take the theme of overall application. I used this approach, but after that application is not being shown on Taskbar anymore. After reading different threads I came to know that because RadWindow is not child of Window so that's why its not possible.
So What is now I am trying to do, is to hide the outer Window completely somehow and to use RadWindow as a child, but parent of all other controls. Following is XAML
<Window x:Class="MyTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
WindowStyle="None"
Title="MainWindow" >
<telerik:RadWindow WindowStartupLocation="CenterScreen" Width="Auto" Height="Auto" x:Name="MyRadWindow">
<Grid>
<!-- All Controls here -->
</Grid>
</telerik:RadWindow>
</Window>
But I am unable to completely hide the outer window. It still shows the borders. Then as a second step I have to handle the minimize, maximize and window close events from this RadWidow.
If anyone have tried this approach please help me out or suggest what would be the better way of doing this?
The main purpose of doing all this is to Sync the GUI of Outerwindow with the current TelerikTheme.
There's a simpler solution using the RadWindowInteropHelper like this:
using Telerik.Windows.Controls.Navigation;
public partial class MyRadWindow : RadWindow
{
public MyRadWindow()
{
InitializeComponent();
RadWindowInteropHelper.SetShowInTaskbar(this, true);
}
}
I think you should try to set main class as telerik window instead of nesting inside normal window:
<telerik:RadWindow x:Class="MyTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
WindowStyle="None" WindowStartupLocation="CenterScreen" ShowInTaskbar="True"
Title="MainWindow" >
<Grid>
<!-- All Controls here -->
</Grid>
</telerik:RadWindow>
Don't forget to change base class of MyTest.MainWindow to RadWindow
EDIT: Sorry didn't notice provided link.
You can try hide main window with overriding it style by setting following attributes:
WindowStyle="None" Background="Transparent" AllowsTransparency ="True"
First open the MainWindow.xaml file and replace the Window declaration with RadWindow declaration:
<telerik:RadWindow x:Class="RadWindowAsMainWindow.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
Loaded="RadWindow_Loaded_1"
Header="MainWindow" Height="350" Width="525">
...
</telerik:RadWindow>
and in code-behind:
`
public partial class MainWindow : RadWindow
{
...
}
2. Then override OnStartup method of the Application class to show the RadWindow:
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
new MainWindow().Show();
base.OnStartup(e);
}
}
`
Then in RadWindowLoad event write this:
`
private void RadWindow_Loaded_1(object sender, RoutedEventArgs e)
{
var window = this.ParentOfType();
window.ShowInTaskbar = true;
}
`

Extracting Propreties to Styles (Xaml processing tool)

I need to extract the props (Height, Width, HorizontalAligment .. etc) of a control to a style.
Do you guys know any tool to do that?
I have tried Xaml Power tools (nice, but only handles xml like attribute properties for ex: is not recognized)
Also looked over expression blend.. didn't find anything there either.
At least some framework/api for easy parsing of xaml (found Xaml Toolkit, but it remained in CTP version in 2010..)
Thanks!
If you have created an element and specified properties such as the Slider control below.
<Window x:Class="Styling.ExtractStyle"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ViewTemplateSource" Height="300" Width="300">
<Window.Resources>
</Window.Resources>
<Grid Name="g1">
<Slider Name="mySlider" Height="100" VerticalAlignment="Center">
<Slider.Width>200</Slider.Width>
</Slider>
</Grid>
</Window>
You can implement an extension to the FrameworkElement class...
public static class FrameworkElementExtensions
{
public static void SaveElementStyleToFile(this FrameworkElement element, string fileName)
{
if (element != null)
{
XmlWriterSettings settings = new XmlWriterSettings
{
Indent = true,
IndentChars = new string(' ', 4),
NewLineOnAttributes = true
};
StringBuilder strbuild = new StringBuilder();
XmlWriter xmlwrite = XmlWriter.Create(strbuild, settings);
if (xmlwrite != null)
{
XamlWriter.Save(element, xmlwrite);
}
File.WriteAllText(fileName, strbuild.ToString());
}
else
{
throw new Exception("Cannot serialize a null object");
}
}
}
And call the extension method...
mySlider.SaveElementStyleToFile("mySliderStyle.xaml");
This will give you an XML file in the root directory of your application which captures the 'hard-coded' properties. Here's what it outputs...
<?xml version="1.0" encoding="utf-16"?>
<Slider
Name="mySlider"
Width="200"
Height="100"
VerticalAlignment="Center"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" />
You can then morph this file into a persistent style with an editor. Note that the target Framework element must be first run through WPF's two pass layout system for this technique to work.
For example
TextBox t = new TextBox();
t.Height = 20;
t.SaveElementStyleToFile("myfile.xml");
will NOT work for that reason. Short of the conveniences offered by Xaml Power Toys and/or a full Xaml parser, this is likely to be as close as you will get to meeting your requirements...

Resources