1

I am having the following code in my Xaml page of WPF project.

<Rectangle Name="myrect" MouseDown="myrect_MouseDown"/> 

I have tried to set the fill property of the rectangle in code behind (i.e. in Xaml.cs) but the name myrect is not accessible from code behind.. what is am missing here.?

I need to change the Fill property when particular rectangle is selected, any ways to achieve it through property binding or styles?

Thanks

4
  • Is that within some Template ? Commented Aug 25, 2016 at 10:50
  • 2
    If you want to access the control by name then you need to use the x:Name syntax (where x is defined xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" in the UserControl/Page. Commented Aug 25, 2016 at 10:52
  • Can you show us the CodeBehind? Commented Aug 25, 2016 at 11:12
  • @user3610920, did you ever get this working? Commented Mar 5, 2018 at 6:10

1 Answer 1

3

In mouse down event you have parameter object sender. This is your Rectangle.

Something like that:

private void myrect_MouseDown(object sender, EventArgs e) { var rect = sender as Rectangle; if(rect!=null) { //do it here... } } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.