I am using InkCanvas control and I want to add a textbox in it as a child in specific position I succeeded to add it but not in the correct position ? how I can transform it or any another method ?
You can use the attached properties Left and Top like so:
var myTextBox = new TextBox();
myCanvas.Children.Add( myTextBox );
InkCanvas.SetLeft( myTextBox, someLeftValue );
InkCanvas.SetTop( myTextBox, someTopValue );
The InkCanvas isn't a panel type (note it inherits from FrameworkElement), but it does contain attached properties (as Ed S. mentioned) that let you position children as though it were a Canvas panel:
<InkCanvas>
<TextBox InkCanvas.Top="50" InkCanvas.Left="50"/>
</InkCanvas>
Alternatively, you can also insert a panel as the child of the InkCanvas. For example:
<InkCanvas>
<Canvas>
<TextBox Canvas.Top="50" Canvas.Left="50"/>
</Canvas>
</InkCanvas>
Related
I am new to C# and WPF. I wanna add the following element in the page.
<Label HorizontalAlignment="Left" Width="99" MouseMove="Variable_MouseMove" x:Name="VariableLabel">
<Run
Background="red"
Text="Variable"
x:Name="Variable"
/>
</Label>
I wanna add it dynamicly to the Canvas, like that:
Label label = new Label();
Run r = new Run();
label.Children.add(r); // error: label doesn't have the definition of Children
canvas.Children.add(label);
I want to set the background color behind only the text, rather than the whole label, so I use Run as Label child element and set the child element's background.
What should I do? Thanks!
The label don't have Children but in you case you can use Content. So
label.Content = run;
will work
I am new to WPF, I wanted to display a window with multiple user controls.
example.xaml
<DockPanel>
<Border x:Name ="TopRegion" DockPanel.Dock = "top">
<local:userControl1/>
</Border>
<Border x:Name ="leftRegion" DockPanel.Dock = "left">
<local:userControl2/>
</Border>
</DockPanel>
The usercontrol1 and usercontrol2 are other views in the same project. I.e usercontrol1.xaml and usercontrol2.xaml.
Problem is that i need to change the usercontrol of leftRegion from usercontrol2 to usercontrol3 during run time i.e programatically.
How to achieve this in example.xaml.cs program.
You already named the Border leftRegion, so you could use this Border to set a new child like
leftRegion.Child = new userControl3();
programatically in code behind.
This means you are replacing the instance of userControl2 of the Border with a new instance of userControl3.
While this is a legitimate request, it crosses with the logic of XAML and data binding, so I would suggest an alternative way that will have the same effect for end-user but is more in the spirit of XAML.
The solution is simple - just have both controls in your XAML and switch their Visibility based on whether you need one or another control to be displayed.
I'd like to move an element from one grid into another and have a problem to assign programmatically a template to the new instance. Further, details of my attempt.
For this purpose, I create an instance of the class together with its visual appearance from the template.
Inside the Window tag I declare the namespace:
xlmns:my="clr-namespace:myNameSpace"
I have a template in resources:
<ControlTemplate x:Key="templateX">
<StackPanel>
<Image Source="pic.png" Width="50" Height="50"/>
</StackPanel>
</ControlTemplate>
and place the element into the grid.
<Grid Grid.Row="2">
<StackPanel>
<my:someClass Template="{StaticResource templateX}" MouseMove="_event">
</StackPanel>
</Grid>
Now, I drag the element, the event "_event" fires. If I push a standard element (e.g. Rectangle) through this, I do the following at the end of the drag-n-drop chain of events:
Rectangle new_instance = new Rectangle();
// place for rectangle's form and color
NewPlace.Children.Add(new_instance);
// place for positioning the rectangle in NewPlace canvas
How, can I do the last part with the element of someClass? If I do
someClass new_instance = new someClass();
NewPlace.Children.Add(new_instance);
the template "templateX" isn't assigned to it.
The issue in this case seems to be that you want to combine two things:
an instance of your custom class (new_instance)
a control template available as a XAML resource
You already know how to create the instance of your class and how to add it to the Children list.
How to retrieve the control template (or for that matter, any other object) from a XAML resource has been discussed in other SO questions, e.g.:
How can I access ResourceDictionary in wpf from C# code?
Accessing a resource via codebehind in WPF
This leads to:
ControlTemplate template = (ControlTemplate)this.FindResource("templateX");
Now, the crucial point is that you do not want to add the control template itself to the Children list. The control template is just a set of instructions how to create a UI tree for your control and bind its properties to those of your control, where appropriate.
Instead, you want to configure new_instance to use the control template you retrieved from the resource. You can do that by assigning the control template to the Template property of new_instance:
new_instance.Template = template;
Once new_instance is added to Children, it will be displayed and it will use your custom control template.
I'm trying to remove the border of a WindowFormsHost that I'm using to host PDFTron's PDFViewCtrl.
Unfortunately, the WindowsFormsHost doesn't have a BorderThickness, or the ability to change the WindowStyle.
In my XAML I have the WindowsFormHost,
<WindowsFormsHost Name="wfHost" Grid.Row="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Loaded="wfHost_Loaded"/>
then later in code I set the child to be the PDFViewCtrl.
pdfViewCtrl = new PDFViewCtrl();
pdfViewCtrl.BackColor = Color.WhiteSmoke;
wfHost.Visibility = Visibility.Visible;
wfHost.Child = pdfViewCtrl;
You can see more information about the PDFViewCtrl here: http://www.pdftron.com/pdfnet/docs/PDFNet/html/T_pdftron_PDF_PDFViewCtrl.htm
public class PDFViewCtrl : Control
where Control is a System.Windows.Forms.Control.
Any ideas?
I use WindowsFormsHost in my WPF projects. It doesn't have a visible border. What you need to do is to set the hosted control's BorderStyle to None.
How can I add an object (in shape of rectangle for instance) to an embedded in a stack panel tag.
I was able to show the objects (as rectangle) on it using and and then using the style in an xaml file using something like:
<stackpanel ...>
<Expander Header="Controls" Content="{StaticResource FC}" IsExpanded="True"/>
</StackPanel>
but need to insert object onto toolbox dynamically. any idea how this could be done through code?
Your help will be appreciated.
Amit
Expander is a type of control which is considered to be a content type control. As such it only has one child ( the child I believe you are calling a 'toolbox') which according to your Xaml is the FC item. Do these steps to add your item to the FC item:
In Xaml give a name to the Expander so it can be accessed in code behind.
In codebehind get the expander via its name.
Access the property Content on the expander and cast it to the FC class.
Add the item you want into the FC's children or content (whatever it is you know and we don't so its my best guess).