tags:

views:

92

answers:

2

Hi I would like to design a system interface where I have different controls(rectangles) animated(light up or glow) one by one and send a message to a Queue as they animate - I am new to c# and wpf so not quite sure how to appoach this - any starting points?

Thanks,

Richard

A: 

Hi Val, thanks for the example - although not full understanding it I have came up with the following but still having some issues:

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; using System.ComponentModel;

namespace WpfApplication1 { /// /// Interaction logic for MainWindow.xaml /// // public partial class MainWindow : Window //{ // public MainWindow() //{ // InitializeComponent(); //DataContext = this; //}

//}

public class Glow : INotifyPropertyChanged
{
    private double m_glowSize;
    public event PropertyChangedEventHandler PropertyChanged;
    public double GlowSize
    {
        get { return m_glowSize; }
        set
        {
            m_glowSize = value;
            NotifyPropertyChanged("GlowSize");
        }
    }
    public void Window()
    {
        InitializeComponent();
        DataContext = this;
    }

    private void Canvas_MouseMove(object sender, MouseEventArgs e)
    {
        Canvas canvas = sender as Canvas;
        if (canvas != null)
        {
            Point mousePosition = e.GetPosition(canvas);
            GlowSize = 20 * (mousePosition.X / canvas.ActualWidth);
        }
    }

    private void NotifyPropertyChanged(string s)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(s));
    }
}

}

the issue seems to be on this chunk:

public void Window() { InitializeComponent(); DataContext = this; } what does DataContext = this; and how do I set it up properly?

Thanks

+1  A: 

Hey Richard, I have retracted my previous answer and updated this new one, hopefully it makes more sense. Additionally please use the "comment" option if you would like more information about a proposed answer.

Basically I've just added a rectangle with a glow effect to a canvas and then bound the size of the halo to a property that I manipulate every time the mouse moves over the Canvas.

Please note, for this to work your code behind class will need to implement the INotifyPropertyChanged interface which is in the System.ComponentModel namespace. You will also need to make sure the datacontext of the window is set correctly.

The content of my Window XAML:

<Canvas Background="DarkGray"
        MouseMove="Canvas_MouseMove">
    <Rectangle Margin="40,40,0,0"
               Width="200"
               Height="200"
               Fill="Gray"
               Stroke="Black"
               StrokeThickness="2">
        <Rectangle.BitmapEffect>
            <OuterGlowBitmapEffect GlowColor="Goldenrod"
                                   GlowSize="{Binding Path=GlowSize}"/>
        </Rectangle.BitmapEffect>
    </Rectangle>
</Canvas>
</Window>

Code Behind for my Window:

public class Window1 : Window, INotifyPropertyChanged
{
    private double m_glowSize;
    public double GlowSize
    {
        get { return m_glowSize; }
        set
        {
            m_glowSize = value;
            NotifyPropertyChanged("GlowSize");
        }
    }

    public Window1() //this is my class constructor
    {
        DataContext = this;
        InitializeComponent();
    }  

    private void Canvas_MouseMove(object sender, MouseEventArgs e) 
    {
        Canvas canvas = sender as Canvas;
        if (canvas != null)
        {
            Point mousePosition = e.GetPosition(canvas);
            GlowSize = 20 * (mousePosition.X / canvas.ActualWidth);
        }
     }    

    private void NotifyPropertyChanged(string s)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(s));
    }
}

As a very basic interpretation of DataContext, you can think of it as the object to which the bindings will seek their bound properties. In this instance we want to make sure that the bindings in our window's XAML are found in its code behind file.

Also, if you haven't already. Take a look at this http://msdn.microsoft.com/en-us/library/aa970268.aspx

I found it very helpful when I first started

Hope it helps.

Val
Hi Val, this helped as a start up - my problem is still how and when to call my class that send messages to a Queue. I also need to animate one rectangle at a time say at the click of a button. Problem is I have to present this to use this work in about three weeks - so time to learn is not there really. But you helped in starting up when I have more time.cheers
I'm not 100% sure what you mean by queue. Do you just want each rectangle to be able to display a message in a given location when you mouse over it? If you want to change the rectangles animation to onclick, you can use the MouseLeftButtonUp event instead of MouseOver.Otherwise if all you want is an animated sequence that responds to a single button, maybe you would do better with another language like Flash
Val
Hi Val, let me explain. I have a Message QUeue(MSMQ) not sure if you're familiar with this(kind of storage if you like) I am simulating a train movement around a track. I have a method that add to that message queue each time the rectangle changes colour(meaning the train is there). The animation is working now(thanks) but need to trigger messages(i.e: am here at rectangle ID:1) - can this be done?I can send the code if you like.
You might want to try DataTemplating the items in an itemscontrol which has a collection of string "locations" as the rectangles. Some examples http://msdn.microsoft.com/en-us/library/ms742521.aspxhttp://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.itemcontainerstyle.aspx, Best of luck.
Val