C # Help dynamically change property value (Reflection)
I am trying to write a simple config in C # that reads XML and configures components (buttons, tabs, etc.) accordingly. My first catch is calling component, attribute and value from variables. Here is a small working snippet, but not how I want it to work since Im not passing the component name dynamically. (A "Blank" button has been added to the 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 (above work), here (below does not work) as far as I got and I am stuck, I am trying to call name, attribute and value all from variables (coming 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 explained what to them after OK ... Thanks for taking the time to read :)
source to share
You are trying to set a property Content
to a string (in this case, a letter "btnSample"
.
I think you really want to look at a button with that name. If you defined it in XAML with x:Name="btnSample"
, you can view it through that name and pass the element itself, not a string:
private void btnSample_Click(object sender, RoutedEventArgs e)
{
object element = FindName("btnSample");
SetProperty(element, "Content", "Clicked");
}
More information on FrameworkElement.FindName (string) at MSDN .
source to share