Click here to Skip to main content
16,020,313 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I created simple project "WpfApplication1" and inserted Canvas "myCanvas" into XAML and I tried to use "myCanvas" inside that class "TestClass" but couldn't, Please help me...

how I can call "myCanvas" inside "TestClass"?
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WpfApplication1
{
    class TestClass
    {
    }
}

XML
<Window x:Class="WpfApplication1.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>
        <Canvas Name="myCanvas" HorizontalAlignment="Left" Height="137" Margin="122,90,0,0" VerticalAlignment="Top" Width="277" Background="Green"/>
    </Grid>
</Window>

C#
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 WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
}
Posted

Thanks all but I write this staff.

We need constructor
C#
public TestClass(Mainwindow window)
{
   window.myCanvas.Background = Brushes.Red;
}

And write on MainWindow
C#
var obj = TestClass(this);

And All is Done.
 
Share this answer
 
Hello,

you can not directly access your canvas object in other class you have to create public property inside the MainWindow.

C#
namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }

//here you can set any property value of your canvas. this is sample demonstrate code how to access not complete running example anymore.
    public Control myCanvas { get; set;}
}
 
Share this answer
 
Comments
suro_sh 6-Jun-13 9:23am    
public Control Get_Canvas
{
get { return myCanvas; }
}

But I can't access this function in the TestClass.
You need a reference to your MainWindow. You can get the reference through Application.Current.MainWindow. All you have to do is cast it to MainWindow type:
C#
var canvas = ((MainWindow)Application.Current.MainWindow).Get_Canvas;


However I don't think that accesing controls from another type is a good idea. You should reconsider why you need a reference to 'myCanvas' from another type. There is a reason why objects instantiated in xaml are private.

Hope that helps.
Uros
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900