C# "Controls" Does Not Exist in The Current Context - c#

Error
In my application I'm trying to use "Controls.Add", but Visual Studio keeps giving me the error:
"Error 1 The name 'Controls' does not exist in the current context
C:\Users\Admin\Desktop\Demo\Demo\MainWindow.xaml.cs 42 13 Demo"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Demo
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Window1 window = new Window1();
Button button = new Button();
window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
button.Content = "New Button";
button.Visibility = Visibility.Visible;
button.Height = 50;
button.Width = 200;
Controls.add(button); <-- ERROR IS FOUND HERE
window.Show();
}
}
}
I'm using WPF, coming from WinForms. Any advice is appreciated, thanks.

You need to do something along the lines of:
var window = new Window1();
var stackPanel = new StackPanel { Orientation = Orientation.Vertical };
Button button = new Button();
button.Content = "New Button";
button.Visibility = Visibility.Visible;
button.Height = 50;
button.Width = 200;
stackPanel.Children.Add(button);
window.Content = stackPanel;
window.Show();
Although, I'd recommend defining all UI components in the XAML and reading up on the MVVM pattern.

<StackPanel Name=“splMain“>
<Button Name=“btnAddMore“ Click=“btnAddMore_Click“>Add Another</Button>
Also on button click :
System.Windows.Controls.Button newBtn = new Button();
newBtn.Content = “A New Button”;
splMain.Children.Add(newBtn);

Button SaveButton = new Button();
SaveButton.Name = "Save";
SaveButton.Content = "Save";
Grid1.Children.Add(SaveButton);

Related

invalidoperationexception In the Begin method of Save The Humans Game

I started learning C# using the book Head First C# and i'm doing the game in chapter 1 using WPF project , the thing is i think i followed everything according to the book but i get a invalidoperationexception in the begin method
here is the code i wrote :
using System;using
System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;`
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
Random random = new Random();
public MainWindow()
{
InitializeComponent();
}
private void startButton_Click(object sender, RoutedEventArgs e)
{
AddEnemy();
}
private void AddEnemy()
{
ContentControl enemy = new ContentControl();
enemy.Template = Resources["EnemyTemplate"] as ControlTemplate;
AnimateEnemy(enemy,0,Play_Area.ActualWidth-100,"(Canvas.left)");
AnimateEnemy(enemy, random.Next((int)Play_Area.ActualHeight - 100), random.Next((int)Play_Area.ActualHeight - 100), "(Canvas.Top)");
Play_Area.Children.Add(enemy);
}
private void AnimateEnemy(ContentControl enemy, double from, double to, string propertyToAnimate)
{
Storyboard storyboard = new Storyboard () { AutoReverse = true, RepeatBehavior = RepeatBehavior.Forever };
DoubleAnimation animation = new DoubleAnimation()
{
From = from,
To = to,
Duration = new Duration(TimeSpan.FromSeconds(random.Next(4, 6))),
};
Storyboard.SetTarget(animation, enemy);
Storyboard.SetTargetProperty(animation, new PropertyPath(propertyToAnimate));
storyboard.Children.Add(animation);
storyboard.Begin();
}
}
}
Here is a picture of my document Outline : Document Outline
why do i get the exception ? can anyone help me ?
Property names are case-sensitive. Try to change Canvas.left to Canvas.Left with an upper-case L:
AnimateEnemy(enemy,0,Play_Area.ActualWidth-100,"(Canvas.Left)");

How to bind panels with controls in devexpress docklayoutmanager

File Name SampleProject
My xaml file Mainwindow.xaml----------
<Window x:Class="SampleProject.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dxdo="http://schemas.devexpress.com/winfx/2008/xaml/docking" Closing="Window_Closing"
Title="MainWindow" Height="800" Width="800" WindowState="Maximized">
<Grid>
<dxdo:DockLayoutManager x:Name="docklayoutmanager1" dxdo:RestoreLayoutOptions.RemoveOldPanels="False" dxdo:RestoreLayoutOptions.RemoveOldLayoutControlItems="False" Margin="0,50,0,0">
</dxdo:DockLayoutManager>
<Button Width="100" VerticalAlignment="Top" HorizontalAlignment="Center" Height="30" x:Name="ClickMe" Content="Click Me" Click="ClickMe_Click" Margin="323,10,369,729" />
</Grid>
</Window>
`
My xaml.cs file MainWindow.xaml.cs-------------------
using DevExpress.Xpf.Docking;
using DevExpress.Xpf.Layout.Core;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace SampleProject
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
if(File.Exists("test.xml"))
{
docklayoutmanager1.RestoreLayoutFromXml("test.xml");
}
}
private void ClickMe_Click(object sender, RoutedEventArgs e)
{
var panel = docklayoutmanager1.DockController.AddPanel(DockType.None);
panel.Caption = "Test Panel";
panel.Name = "myPanel";
panel.Content = "I want to serialize this code using guid Please provide example for creating such binding.";
FloatGroup floatgroup = new FloatGroup();
floatgroup.Name = "myFloatGroup";
floatgroup.FloatSize = new Size(500,500);
floatgroup.FloatLocation = new Point(300, 300);
floatgroup.Add(panel);
docklayoutmanager1.FloatGroups.Add(floatgroup);
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
string xmlFilePath = System.AppDomain.CurrentDomain.BaseDirectory + "test.xml";
if (File.Exists("test.xml"))
{
docklayoutmanager1.SaveLayoutToXml("test.xml");
}
else
{
using (File.Create("test.xml")) { }
docklayoutmanager1.SaveLayoutToXml("test.xml");
}
}
}
}
I have a document layout manager programmatically getting panels and controls which is having panels and controls in them.
While saving, I am achieving this with SaveLayoutToXml() for my panels and Serializing controls using bformatter.Serialize();.
since i have lot of panels and unique controls in them. I want to get the same controls back in same panels as it was before saving and serializing. Please provide me with a code to identify with a unique id for both panels and controls.
And do I have any integer id to which I can assign GUID as BindableName does not work with it.
Thanks
Desh
Create your panels programmatically and and them to your xaml file at run time.
UserC = new UserC();
DocumentPanel dpanel = new DocumentPanel();
dpanel.Name = "OpenUC" + count;
dpanel.Caption = "Open User Control";
dpanel.BindableName = "OpenUC" + count;
var content = UserC;
dpanel.Content = content;
FloatGroup fGroup = new FloatGroup();
fGroup.FloatSize = new Size(500, 500);
fGroup.Items.Add(dpanel);
DockLayoutManager1.FloatGroups.Add(fGroup);
This is how to add a User Control, since nobody seems to be interested in helping, i searched and done something like this:
private FloatGroup CreateDocPanelFloatGroup(DocumentPanel dp)
{
FloatGroup floatGroup = new FloatGroup();
floatGroup.FloatSize = new Size(500, 500);
floatGroup.Items.Add(dp);
return floatGroup;
}
private void OpenUC_ItemClick(object sender, DevExpress.Xpf.Bars.ItemClickEventArgs e)
{
openUC = new OpenUC();
DocumentPanel dp = new DocumentPanel();
dp.Name = "OpenUC" + count;
dp.Caption = "Open User Control";
dp.BindableName = "OpenUC" + count;
var content = openUC;
dp.Content = content;
DockLayoutManager1.FloatGroups.Add(CreateDocPanelFloatGroup(dp));
}

Grid Lines wont show

Grid lines wont show on runt-time I can seem to see where I am going wrong. This is a WPF window application. I just want to generate columns and rows using C# code. Any ideas why this is not working?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace generator
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void CreateGrid()
{
ColumnDefinition myColum =new ColumnDefinition();
RowDefinition myRow = new RowDefinition();
ColumnDefinition myColum1 = new ColumnDefinition();
RowDefinition myRow1 = new RowDefinition();
Grid myGrid = new Grid();
myRow.Height = new GridLength(1, GridUnitType.Star);
myColum.Width = new GridLength(1, GridUnitType.Star);
myRow1.Height = new GridLength(1, GridUnitType.Star);
myColum1.Width = new GridLength(1, GridUnitType.Star);
myGrid.ColumnDefinitions.Add(myColum);
myGrid.RowDefinitions.Add(myRow);
myGrid.ColumnDefinitions.Add(myColum1);
myGrid.RowDefinitions.Add(myRow1);
myGrid.ShowGridLines = true;
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
CreateGrid();
}
}
}
The question may as well read "Grid won't show".
You are creating a new Grid instance, but you never add it to the visual tree. You may assign it to the Window's Content property:
private void CreateGrid()
{
...
Content = myGrid;
}

How to convert this code-based WPF tooltip to Silverlight?

The following ToolTip code works in WPF.
I'm trying to get it to work in Silverlight.
But it gives me these errors:
TextBlock does not contain a definition for ToolTip.
Cursors does not contain a definition for Help.
ToolTipService does not contain a definition for SetInitialShowDelay.
How can I get this to work in Silverlight?
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
namespace TestHover29282
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
AddCustomer("Jim Smith");
AddCustomer("Joe Jones");
AddCustomer("Angie Jones");
AddCustomer("Josh Smith");
}
void AddCustomer(string name)
{
TextBlock tb = new TextBlock();
tb.Text = name;
ToolTip tt = new ToolTip();
tt.Content = "This is some info on " + name + ".";
tb.ToolTip = tt;
tt.Cursor = Cursors.Help;
ToolTipService.SetInitialShowDelay(tb, 0);
MainStackPanel.Children.Add(tb);
}
}
}
Tooltips are added to Silverlight controls using an attached property provided by the ToolTipService. There is no SetInitialShowDelay in Silverlight's version nor is there a Help cursor on the Cursors type.
void AddCustomer(string name)
{
TextBlock tb = new TextBlock();
tb.Text = name;
ToolTip tt = new ToolTip();
tt.Content = "This is some info on " + name + ".";
ToolTipService.SetToolTip(tb, tt);
MainStackPanel.Children.Add(tb);
}

How can I make a WPF ToolTip appear faster with ToolTipService.Duration?

How can I make the Tooltip in the following code display faster?
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
namespace TestHover29282
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
TextBlock tb = new TextBlock();
tb.Text = "Jim Smith";
ToolTip tt = new ToolTip();
tt.Content = "This is some info on the customer.";
tb.ToolTip = tt;
//ToolTipService tts = new ToolTipService();
//tts.Duration = 0;
//tb.ToolTipService = tts;
MainStackPanel.Children.Add(tb);
}
}
}
ToolTipService.SetInitialShowDelay(tb, 10);

Categories