tags:

views:

122

answers:

1

Hello,

I'm trying to write a simple config in C# that reads XML and customizes the components (Buttons, Tabs ETC) accordingly. My first snag is calling a component, attribute and value from variables. Here is a small working snippet but not how I want it to work as I’m not passing the component name dynamically. (Dummy button added to Canvas called btnSample)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
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 WpfApplication5
{
  public partial class Window1 : Window
  {
    public Window1()
    {
        InitializeComponent();                       
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
    }

    private void SetProperty(object target, string propertyName, object value)
    {
        PropertyInfo property = target.GetType().GetProperty(propertyName);
        property.SetValue(target, value, null);
    }

    private void btnSample_Click(object sender, RoutedEventArgs e)
    {
        SetProperty(btnSample, "Content", "Clicked");
    }
  }
}

This is not 100% what I want it to (above working), here (below not working) is how far I've got and I'm stuck, I’m trying to call name, attribute and value all from variables (originating from LINQ/XML), any help would be very helpful :)

using System;
using System.Collections.Generic;  
using System.Linq;
using System.Reflection;
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 WpfApplication5
{
  public partial class Window1 : Window
  {
    string tar;

    public Window1()
    {
        InitializeComponent();                       
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
    }

    private void SetProperty(object target, string propertyName, object value)
    {
        PropertyInfo property = target.GetType().GetProperty(propertyName);
        property.SetValue(target, value, null);
    }

    private void btnSample_Click(object sender, RoutedEventArgs e)
    {
        tar = "btnSample";
        SetProperty(tar, "Content", "Clicked");
    }
  }
}

I think i've explained what im after ok... Thanks for taking time to read :)

+1  A: 

You are trying to set the Content property on a string (in this case, the literal "btnSample".

I think what you really want to do is look up the button with that name. If you defined it in XAML with x:Name="btnSample" then you can look it up via that name, and pass the element itself rather than the string:

private void btnSample_Click(object sender, RoutedEventArgs e)
{
    object element = FindName("btnSample");
    SetProperty(element, "Content", "Clicked");
}

More information on FrameworkElement.FindName(string) on MSDN.

Drew Noakes
Have just re-read your question. Give me a second to re-write my answer.
Drew Noakes
I've just updated the answer to do what you're asking. Hope that helps.
Drew Noakes
Thank you Drew!
Dan Bater
No worries. Welcome to Stack Overflow btw :)
Drew Noakes