0

I am working with animations using Blend.
I created a storyboard, which changes the margin of a button to be on the exact same position as one of my 55 textboxes that are placed on a grid.
The animation should move the button to one of the 55 textboxes depending in on which textbox I click
I created an array which consists of all the margins from the 55 textboxes and I now want to use this array in xaml.
I tried to use Resources.Add and StaticResource but it is not working.

2
  • You have a layout of 55 TextBoxes only by their Margins? That's sounds odd. Think about using appropriate layout panels, like StackPanel or Grid. Assuming that the TextBoxes are stacked (e.g. vertically), you may place them in a single ItemsControl, and show the Button by setting the Visibility of a Button element in the ItemTemplate. Commented Oct 24, 2016 at 11:59
  • I do have 55 textboxes in a grid, they are placed like a typical school timetable. my objective is that when I press the button it moves itself onto the textbox and dissappears, but to do this animation I need the position from the textboxes i.e. their margins if i am correct. Sorry I did not state the question clear enough Commented Oct 24, 2016 at 12:07

1 Answer 1

1

Using arrays in XAML is quite simple: first you declare your margins array as a public property of your Window:

public Thickness[] MyArray { get; set; }

Then you initialize it in your Window constructor:

MyArray = new Thickness[50];
MyArray[0] = new Thickness(5, 5, 5, 5);
MyArray[1] = new Thickness(10, 10, 10, 10);
// ...
DataContext = this;

And then you bind your buttons' Margin properties to each element of your array:

<TextBox x:Name="TextBox1" Margin="{Binding MyArray[0]}" />
<TextBox x:Name="TextBox2" Margin="{Binding MyArray[1]}" />

Anyway it's a much better idea to use ObservableCollection instead of Array because otherwise your view won't be notified about the changes done inside your array from code behind:

public ObservableCollection<Thickness> MyCollection { get; set; }

public MainWindow()
{
    InitializeComponent();

    MyCollection = new ObservableCollection<Thickness>();
    MyCollection.Add(new Thickness(5, 5, 5, 5));
    MyCollection.Add(new Thickness(10, 10, 10, 10));
    // ...
    DataContext = this;
}

And in your XAML the bindings remain the same:

<TextBox x:Name="TextBox1" Margin="{Binding MyCollection[0]}" />
<TextBox x:Name="TextBox2" Margin="{Binding MyCollection[1]}" />
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.