Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# XAML WPF
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"?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace WpfApplication1
{
    class TestClass
    {
    }
}
<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>
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 6-Jun-13 1:51am
suro_sh519
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Thanks all but I write this staff.
 
We need constructor
public TestClass(Mainwindow window)
{
   window.myCanvas.Background = Brushes.Red;
}
And write on MainWindow
var obj = TestClass(this);
And All is Done.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hello,
 
you can not directly access your canvas object in other class you have to create public property inside the MainWindow.
 
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;}
}
  Permalink  
Comments
suro_sh at 6-Jun-13 9:23am
   
public Control Get_Canvas
{
get { return myCanvas; }
}
 
But I can't access this function in the TestClass.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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:
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
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 400
1 Jochen Arndt 190
2 Richard MacCutchan 135
3 DamithSL 95
4 Garth J Lancaster 90
0 OriginalGriff 6,045
1 DamithSL 4,601
2 Maciej Los 4,032
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,220


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 7 Jun 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100