0

right now I have multiple buttons and other elements. Each of these has a x:Name and a Content.

I also have a list which has elements with the same names as the x:Name of the WPF parts.

My goal is to have a function I can call over and over again, give it the x:name of an element and it sets the value after the name in the list ( {name1, value, name2, value, name3, value ..} )to the content of the WPF element with that name.

For Example:

I have a list:


config{tree, 1, chair, 4, window, 3, dollar, 200}

and WPF elements:
<Button x:Name="chair" Content="12"/> 
<Button x:Name="tree" Content="5"/>

Now I want to call a function, giving it the config{} and x:Name="chair". It should search for an element "chair" in the config now and replace the 4 after that with the 12 from the Content. Now I want to call it with the next Button ( x:Name="tree") to change the value 1 to 5;

I really don't know, how to describe that properly :(

This is the function I have right now:

 public void SetConfig(List<string> config, string name)
        {
            config[config.IndexOf(name) + 1] = myWindow.name.Content.ToString();

        }

The myWindow.NAME.Content.ToString(); part does not work, because I can not use a variable in that.

Is there any way, I can still do this ?

Thanks in advance :D

1stkeks
  • 25
  • 2
  • 7
  • you could try to use the answers from http://stackoverflow.com/questions/636383/how-can-i-find-wpf-controls-by-name-or-type to access your controls. – Herm Nov 27 '14 at 13:46

3 Answers3

0

You could access the buttons properties by casting the sender from an Click event that is bound to several buttons. Like so:

    private void Click(object sender, RoutedEventArgs e)
    {
        Title = ((Button) sender).Name;
    }
Lofbergio
  • 26
  • 2
0

This example works for me. You have to give a name to your Container on the WPF Form (example: a grid), and use the method "FindName(string)", like this:

XAML:

 <Grid Name="LayoutRoot">
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Button x:Name="chair" Content="12" Grid.Column="0" Click="chair_Click"/>
        <Button x:Name="tree" Content="5" Click="tree_Click" Grid.Column="1"/>
    </Grid>

Code Behind:

public partial class MainWindow : Window
    {

        List<string> config = new List<string>() { "tree", "1", "chair", "4", "window", "3", "dollar", "200" };

        public MainWindow()
        {
            InitializeComponent();
        }

        private void tree_Click(object sender, RoutedEventArgs e)
        {
            SetConfig(config, this.tree.Name);
        }

        private void chair_Click(object sender, RoutedEventArgs e)
        {
            SetConfig(config, this.chair.Name);
        }

        public void SetConfig(List<string> config, string name)
        {
            config[config.IndexOf(name) + 1] = ((Button)LayoutRoot.FindName(name)).Content.ToString();

        }       
    }

Pay attention: If you work with MVVM pattern, this is not usable.

Hope it helps.

Stefano Bafaro
  • 915
  • 10
  • 20
  • 1
    This fixed it for me. Thank you very much! I didn't create a grid like you did, but used ((Button)myWindow.FindName(name)).Content.ToString(); – 1stkeks Nov 27 '14 at 14:10
0

It looks as though you are using a List<T> as though it were a KeyValuePair provider, try replacing your List<T> with a Dictionary<T, U>.

Also you can use a single method in your code-behind to manage all Button OnClick events so that you do not have to add a new OnClick handler for each additional Button.

MainWindow.xaml

<Window x:Class="SO.Q_27171497.Wpf.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">
    <StackPanel>
        <Button x:Name="chair" Content="12" Click="Button_OnClick"/>
        <Button x:Name="tree" Content="5" Click="Button_OnClick"/>
    </StackPanel>
</Window>

MainWindow.xaml.cs

namespace SO.Q_27171497.Wpf
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows;
    using System.Windows.Controls;

    public partial class MainWindow
    {
        private readonly Dictionary<string, int> config = new Dictionary<string, int>
        {
            {"tree", 1},
            {"chair", 4},
            {"window", 3},
            {"dollar", 200}
        };

        public MainWindow()
        {
            this.InitializeComponent();
        }

        private void Button_OnClick(object sender, RoutedEventArgs e)
        {
            var button = sender as Button;
            if (button == null)
            {
                return;
            }
            button.Content = this.config.FirstOrDefault(c => String.CompareOrdinal(button.Name, c.Key) == 0).Value;
        }
    }
}
Peppermintology
  • 9,343
  • 3
  • 27
  • 51