Good afternoon everyone. To demonstrate what I'm after, let's say that I have the following classes:
public enum Field {FirstName, LastName, Address, City, State, Zipcode};
public class Item
{
public Field Id {get; set;}
public string Name {get; set;}
public void Item(Field field, string name)
{
Id = field;
Name = name;
}
}
public class Items
{
private List<Item> _Items;
public void AddItem(Field field, string name)
{
_Items.Add(new Item(field, name));
}
public Item GetItem(Field field)
{
foreach(Item item in _Items)
{
if( item.Id == field ) return item;
}
return null;
}
}
public Window SomeForm : Window
{
private Items _Items;
public SomeForm()
{
_Items = new Items();
_Items.Add(Field.FirstName, "First Name");
_Items.Add(Field.Address, "Address");
DataContext = Items;
InitializeComponent();
}
}
And then in the XAML:
<StackPanel Orientation="Horizontal">
<Label DataContext="{Binding GetItem(Field.FirstName)}" Content="{Binding Name}" />
<Label DataContext="{Binding GetItem(Field.Address)}" Content="{Binding Name}" />
</StackPanel>
Ideally, I would like to do something where ControlField is an attached property:
<StackPanel Orientation="Horizontal">
<Label Style="{StaticResource MyLabel}" ControlField="{x:Static Field.FirstName}" />
<Label Style="{StaticResource MyLabel}" ControlField="{x:Static Field.LastName}" />
</StackPanel>
and then the binding of DataContext and label Content would occur in the MyLabel style.
I know GetItem(Field field) won't work (I think) but the following won't work (for several reasons) "{Binding DataContext[Field.FirstName]}".
I have worked with various things to no avail. I have kept my description somewhat high level so describe what I'm trying to accomplish. With this in mind, how I can go about this please? Thank you in advance.