I have two xaml files.
The main.xaml file and a secondary file called test.xaml
In the main.xaml file I use (with a frame) the test.xaml file.
<Frame x:Name="test" Source="test.xaml"/>
All elements have names using the x:names tag.
How do I programatically access the elements from the test.xaml from my main.xaml code behind file?
Zjeriet
How do I programatically access the elements
From your main page code behind cast the source element to the class instance name which test.xaml is based off of and access it in codebehind appropriately.
var myPage = test.Source as {InsertClassNameHere};
Related
I have a XAML file that I want to localize. The following .resx files are inside my Properties directory:
Resources.resx
Resources.de-DE.resx
The key mwjobtitle is defined in both resources files.
I added to XAML:
xmlns:p="clr-namespace:latex_curriculum_vitae.Properties"
Then I added:
<Label Content="{x:Static p:Resources.mwjobtitle}" HorizontalAlignment="Left" Margin="64,52,0,0" VerticalAlignment="Top"/>
While writing p:Resources the autocomplete finds already the entry mwjobtitle, but I'm getting:
Fehler XDG0062 Member 'latex_curriculum_vitae.Properties.Resources.mwjobtitle' not found. latex_curriculum_vitae MainWindow.xaml 12
So I can't compile it now. What can I do?
The complete project can be found there.
In order to access resources in XAML, the access modifer of the .resx files must be Public. You can open the resource file in Visual Studio, and select Public from the access modifier drop-down.
Alternatively, right-click the resource file, select Properties and set the Custom Tool to PublicResXFileCodeGenerator.
I have a tab control with a data template that renders data based on the template and the data on the source it is bound to.
However, this instantly makes UI development harder as I don't see anything in Visual Studio 11, because all data comes when the application is running. Is there some way to add test data that only appears while working with the UI?
This tutorial from MSDN was perfect: http://msdn.microsoft.com/en-us/library/ee823176.aspx
Basically:
Create a file DesignData/FooData/Foo.xaml with content such as:
<local:Foo xmlns:local="clr-namespace:YourProject"
SomeProperty="Sample" AnotherProperty="Bar" />
Note that Foo must be a class that exists under the namespace YourProject with the properties you just used.
Then select the file from the Solution Explorer and set the Build Action to Design Data.
Add a namespace to your MainWindow.xaml code:
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
along with the other namespaces you have.
Then add this to your top-level Grid for instance:
d:DataContext="{d:DesignData Source=./DesignData/FooData/Foo.xaml}"
Now elements below that Grid element (under the influence of DataContext) can have the property:
<TabControl ItemsSource="{Binding Mode=OneWay}">...
In my case I used a tab control and my data context was a collection DesignData/FooData/FooCollection.xaml:
<local:FooCollection xmlns:local="clr-namespace:YourProject">
<local:Foo Prop1="Sample" Prop2="test" />
...
</local:FooCollection>
I need to load an image file to WPF's dynamic resource programmatically because the directory which contains the image files can be moved.
How can I load an image to WPF's dynamic resource which is used like this: Source="{DynamicResource ...}" in XAML?
In XAML:
<Image Source="{DynamicResource MyDynamicImage}" />
In code:
var myimg = new BitmapImage(new Uri("SomeUriHere"));
Resources["MyDynamicImage"] = myimg;
(The Resources collection should be one which is in the scope of the Image of course, if you have a direct reference to the Image control you can also use the immediate Image.Resources)
Whenever you set a new object to that resource key the DynamicResource will update.
I'm working on a wpf application, and up until recently, I had a ResourceDictionary inside my main window's resources part of the xaml. The resource dictionary contained an DataTemplate that was used to style several listboxes in the window. The xaml for this datatemplate contained pointers to event handlers, eg:
<Button n:Name="btnClickMe" Content="Click Me!" LeftMouseButtonUp="btnClickMe_Click" />
I recently decided to split the content of the window up into separate user controls, and to move my ResourceDictionary into it's own file. But, of course, there isn't a code-behind file for a resource dictionary file. How can I wire this up, with things split up as I've described?
Thanks in advance!
You can add a code-behind to a ResourceDictionary; just make sure your class names are referenced correctly. For instance, in the ResourceDictionary if you were working with AppStyles.xaml the XAML file would have a class of:
x:Class="Client.App.Shell.themes.AppStyles"
In the code-behind, AppStyles.xaml.cs, you would make sure to have the class:
namespace Client.App.Shell.themes
{
public partial class AppStyles
...
You can add a new class and name it with the same name as your resource dictionary plus the .cs extension and Visual Studio will automatically set things up so it becomes the code behind file.
For example if you have a resource dictionary called Buttons.xaml, add a file called Buttons.xaml.cs.
You should consider using RoutedCommands, I am thinking.
there are many many resources online, here are a couple that might help you.
http://msdn.microsoft.com/en-us/library/ms752308.aspx
http://www.devx.com/DevX/Article/37893/0/page/1
I have a problem with figuring out how to refer to a Canvas in another XAML file in my project.
For example there is a Canvas object containing some labels, images called MyLayout and is stored in MyLayout.xaml
I want to use this Canvas in the main Window of the application in Window.xaml. How can this be done? There will be multiple layouts that will be needed to be loaded into the Window.
All the XAML is within the project and compiled it cannot be loaded from a file.
How do I reference the Canvas Object in the MyLayout.xaml file in the Window.xaml?
This can be in XAML, VB.NET code or even C#.
I have searched for hours trying to figure this out. How to use an object from one XAML file in another? How is this done?
Thanks in advance. I hope someone knows how to do this.
Have you thought about using a UserControl or ControlTemplate?
Since your Canvas object is really a subclass of Canvas, that subclass is defined in your assembly. You can reference objects defined in other namespaces by adding another xmlns attribute to the root XAML object.
Something like this (assuming that your Canvas subclass is called MyCanvas in the MyNamespace namespace):
<Window x:Class="MyNamespace.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyNamespace">
<local:MyCanvas />
</Window>
You can set the properties of the MyCanvas class like any other object.