How to use a User Control XAML within a Class Library - c#

I have a sub project wich is a class and contains DataLib.cs and an user controll MediumTile.xaml. This user control will be the generated to an image to use it as tile background. But before I have to change a few thing dynamicly. So how can I get controll above the LayoutRoot inside MediumTile.xaml for example to set the background color?
Something like this:
MediumTile.LayoutRoot.Background = new SolidColorBrush(Color.FromArgb(255, 206, 23, 23);

MediumTile.xaml probably exists in some kind of namespace.
You can find the namespace of the UserControl in the top of the file beside the x:Class declaration.
Typically it'll look something like
x:Class="MyProject.UserControls.MediumTile"
if your project is set up normally.
If you look at the MediumTile.xaml.cs, you should see a namespace like so
namespace MyProject.UserControls
{
public partial class MediumTile : UserControl
...
First off, you will need to reference your subproject.
Assuming you have a project structure like so...
CurrentProject/
-MyPage.xaml
SubProject/
-MediumTile.xaml
Right-click your Solution in Visual Studio and click Properties.
Under Properties select Project Dependencies.
Choose the CurrentProject in the dropdown.
In the Depends On checkbox field, choose the SubProject.
Click on StartUp Project in the side bar.
Make sure Single StartUp Project points to CurrentProject. If not, set it.
Now you're done setting up, you'll need to actually use MediumTile.xaml now.
To use the MediumTile UserControl in another XAML file, you will need to declare
xmlns:customControls="clr-namespace:MyProject.UserControls"
inside the page header, and call
<ListBox.ItemTemplate>
<DataTemplate>
<customControls:MediumTile/>
...
To use this UserControl in another CS file, you will need to import the namespace
using MyProject.UserControls;
at the top of the page, and reference your control like so (depending on your usercontrol's constructor),
MediumTile mediumTile = new MediumTile()
About your LayoutRoot problem, you can simply set the Background color directly on the UserControl. UserControl inherits from Control, which has the Background property already.

I've never done it for windows phone 8, but for normal desktop applications, you can do it by adding the following references:
PresentationCore
PresentationFramework
WindowsBase
Then you can create and access a Control in a normal way.

Related

Telerik.WinControls.UI.RadToggleSwitch + coded ui test + c#

I have a telerik control (Telerik.WinControls.UI.RadToggleSwitch) which is used to toggle between state 1 and state 2 with "Click and drag (left or right)" to make the toggle effect. I want to do a hand-coded UI test to select a state and proceed further. I need a class to call that control(i assumed it is WinClient).
I need the proper code to perform toggle action. Thanks in advance.
You may handcode your Coded UI Tests but handcoding your UIControls is really troublesome to do as you can never be sure that the Control you added is actually found and everything you did is correct.
A better alternative is to use the UIMap to manually add your controls via the Coded UI Test Builder. It also saves a lot of time.
When the Coded UI Test Builder is open you can hover with your mouse over a control and type "Control+I" to get info on that specific control. If you now click on the << on the infobox that opens you see your UIMap with the control you did "Control+I" over on the left added. But the control is not added yet permanently. From here you can add it permanently by clicking on the square with the green plus sign.
Add Button Icon
Alternatively the easy way is to hover over a control and push "Control+Shift+I".
See also: https://learn.microsoft.com/en-us/visualstudio/test/use-ui-automation-to-test-your-code
After adding the specific control to the UIMap you may use it by referecing to the UIMap. When coding in the *.cs file of the UIMaps you can reference to it by the "this" statement.
For example:
this.UIWindow.UITitleBar.UICloseButton;
When you want to use it outside of the files of the UIMap you have to create an object of the class of the UIMap and then can use it like above by repacing "this" with the object reference.
For example:
MyUIMapClass uIMapObject = new MyUIMapClass();
uIMapObject.UIWindow.UITitleBar.UICloseButton;
If the file you are coding in is not in the same namespace you have to add a using statement for the namespace of the UIMap (the namespace is defined at the begining of each file in the UIMap).
So for your control I think what you need is Mouse.StartDragging() and Mouse.StopDragging().
public static void StartDragging(UITestControl control);
public static void StopDragging(UITestControl control, int moveByX, int moveByY);
So a dragging towards left would be:
Mouse.StartDragging(UIYourControl)
Mouse.StopDragging(UIYourControl, -20, 0);
And toward right:
Mouse.StartDragging(UIYourControl)
Mouse.StopDragging(UIYourControl, 20, 0);
You should test a bit with the amount you need to drag in each direction for it to register as dragging but I think -20 and 20 should be fine.
I hope I helped a bit. :)

How do I mark a control as 'Private' in WPF?

With WinForms programs I've become accustomed to marking the Modifiers property of a control as 'Private' to prevent external classes and whatever else have you from being able to see and mess with them.
Being still very green with WPF, I see no obvious equivalent in WPF that allows me to make it so external classes cannot see a control I drop onto a form or another user control or what not. I did notice something of x:FieldModifier = "Private" but I get the error "x:FieldModifier = "Private" is not valid for the language C#".
How do I mark a control as Private so it cannot be viewed or accessed by external class objects?
TL;DR
Most of the time you don't need to worry about this in WPF. However:
If you name a XAML element using the x:Name attribute, then you can use the x:FieldModifier attribute to control the visibility of the auto-generated field representing that element. This attribute value is language- and case-specific.
If you don't name a XAML element, then don't bother using the x:FieldModifier attribute.
Read on for a more detailed explanation.
Explicit naming and generated fields
If you create a new WPF application project in Visual Studio, it will create a MainWindow class, the XAML for which looks something like this:
<Window x:Class="StackOverflow.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">
<Grid>
</Grid>
</Window>
If you look at the code-behind class for this window, it will look like this:
// Several using statements...
namespace StackOverflow
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}
Note the use of the partial keyword to denote this as a partial class. If you navigate to the project's obj\Debug folder using Windows Explorer, you will find a file called MainWindow.g.cs: it is this file that contains the code generated by the IDE from your XAML (it is basically the equivalent of the *.Designer.cs file from WinForms).
Your window has a Grid on it, but note that it is not surfaced directly anywhere in the code for MainWindow. Now edit your XAML to give the Grid a name:
<Grid x:Name="_myGrid">
Compile the application, and open the MainWindow.g.cs file again. You will see that the following line has been added:
internal System.Windows.Controls.Grid _myGrid;
Setting the x:Name property of the element in the XAML has caused the code generator to add a field with that name. The field is marked as internal which means it is accessible to all types in your project, but not to any other projects that reference your project.
So basically, if you do not explicitly name an element in the XAML using the x:Name attribute, the code generator will not create a named field for the element in the code-behind class, and your element will effectively be private (this means that the class itself cannot access the element directly either).
Nameless UI elements can still be accessed from code (if you have an instance)
An element without a name can still be accessed via code, by "walking" the visual tree of a Window instance. For example, because the window's content is set to a single Grid element, you can access that grid through code like so:
Grid grid = (Grid) this.Content;
this here refers to the MainWindow class instance.
WinForms has exactly the same "problem" as WPF in this regard: even controls that are not explicitly named can still be accessed through code. Imagine a WinForms Form with a single Button control on it. You can access that button like so:
Button button = (Button) this.Controls[0];
The fact that the button had a default Modifiers value of "Private" did not stop the code from being able to access it.
The FieldModifier attribute controls generated field visibility
Coming back to WPF, particularly if you're using the Model-View-ViewModel (MVVM) pattern, you will rarely need to explicitly name your elements in the XAML, hence the default behaviour will be fine. However, if you do find that you need to name your XAML elements, and you wish to "hide" these elements, then you can use the x:FieldModifier attribute to set the visibility of an element to private instead of the default internal. The value used for the attribute is language-dependent and case-sensitive, eg. for C#:
<Grid x:Name="_myGrid" x:FieldModifier="private">

How to create White CustomUIItem with DockPanel root?

I have custom controls set in my window with a complicated structure.
I'd like to create a custom control to incapsulate all logic of this control (with inner grids, buttons, etc.).
In a XAML i've added:
<DockPanel AutomationProperties.AutomationId="WidgetName">
In a source code i've added:
[ControlTypeMapping(CustomUIItemType.Custom)]
public class MyWidget : CustomUIItem{}
Add now i'm trying to find the item:
_window.Get<MyWidget>("WidgetName");
It throws with error that couldn't find control with Custom type and 'WidgetName' name.
Also there will be a set of such controls in a window. Is there something like
_window.GetAll<> instead of .Get<>?
Try to use Window.Get(SearchCriteria.All);

Modify Resource in Design Time, with code behind in WPF project

What I'm trying to do right now: Modify the Expression Blend UI / Visual Studio, to add a button on one of my dependency properties, and when I click on it, it creates a new trigger.
What is working: I created the button, it appears in the UI, that's fine.
What is not working: I cannot modify the Resource to add a trigger (if I step in, it works but it does not modify on the global resource, only on the instance I think).
I have my main project in Visual Studio, and a property with a button like this:
When the button is pressed this is what happens:
I get my Control that contains that dependency property (Ok).
I searched for the Resource file that contains the Resource I want to modify (Ok).
I update the Resource, but it does not replace the Resource on disk.
I think that it's because I only modify it on memory, so it's in the "air"
I don't know where to go now... I need help
The code behind where I modify the Resource is in an other DLL, the MyLibrary.Design.cs
I'm using Visual Studio 2010 / Blend 4 / .NET 4.0
That's one splution I can figure at the moment, but unfortunately I'm not able to test it now. Still you can check if it works for you.
You can have a class, say ResourceContainer.cs, and collect your control in it as a public static value, and let your control be a button:
public static Button MyButton;
Then you can use it in your code-behind:
If your window is MainWindow.xaml, and, say you need your particular control in a grid, then you probably have something like:
<Window x:Class=...
...(namespace stuff)...
Title="MainWindow" ...(size stuff)...>
<Grid x:Name="MyGrid">
...(your code here)...
</Grid>
...
</Window>
Then in MainWindow.xaml.cs you can use ResourceContainer.cs like this:
...
ResourceContainer rc;
...
MyGrid.Resources.Add("MyKey", rc.MyButton);
...
(when you need it)
rc.MyButton.Triggers.Add(TriggerBase item);
...

How do i change the status of check box and to change text block's value of already opened parent window

Im developing an application in C# under WPF.I want to change the status of the check box and also i need to change the text block's value of already opened window from the currently working windows operation and to update that opened window with these changes(Refresh the already opened window with some updates).
In order to control UI elements from code behind you need to assign a name to each UI element you would like to control.
As for check box decleared as
<CheckBox Name="chkA"> Checkbox A </CheckBox>
you can change its' checked state from code-behind via
chkA.IsChecked = true;
As for the diffenet window update - your Windows in WPF are just classes, part of which usually lives in *.xaml file, and another in the corresponding *.cs file.
If you declare a public method that refreshed the windows content's as you want in your second windows' class, and, when you will be creating your second window, you somehow save the reference to its' instance available in a first class (or some other logic in your application), you will be able to simply call that method from windows' 1 code to refresh the second widows appearance, as declared in a method called.
Basically, from Windows1 you call:
MySecondWindow secW = new MySecondWindow();
secW.Show();
....
secW.RefreshWithMyChages();
RefreshWithMyChages() is just a public method in your second windows' class codebehind.
All of this hold true if:
both of your windows are in the same project
you are not willing to use MVVM or other UI-patterns.

Categories

Resources