I have a combobox which is structured like this:
public class PersonDataQueryField
{
public string FriendlyName { get; set; }
public IList<string> XPaths { get; set; }
public Type DataType { get; set; }
}
A list of this class exist in my ViewModel, and bound to a Combobox, with the DisplayMemeberPath=FriendlyName. I have several DataTemplates in my View, which will trigger based upon the selected DataType in the Combobox to fill the ContentControl. To the right of this combobox a TextBox exist where the user can enter a SearchQuery. The Text property is of type object and meant to be casted to the SelectedPersonDataQueryField.DataType when the user presses "Search".
var searchQuery = (SelectedSearchQueryParameter.DataType) SearchQuery;
This is not allowed since SelectedSearchQueryParameter is a Property and not a Type, but the DataType is Type. How can I achieve this?
I'd agree with Jon. Though, this is how:
dynamic searchQuery = Convert.ChangeType(SearchQuery,
SelectedSearchQueryParameter.DataType);
but why, and what's next :p?
Related
I have a question that confuses me a little bit: I want to ask about dynamic columns building in ListView/DataGrid in WPF(Xaml). I mean, the number of columns is not determined and can change over time. For example:
public class LocalizationModel
{
public string Key { get; set; }
public List<LocalizationCell> Translation { get; set; }
}
public class LocalizationCell
{
public CultureInfo Culture { get; set; }
public string Translation { get; set; }
public LocalizationCell(CultureInfo culture, string translation)
{
Culture = culture;
Translation = translation;
}
}
and in ViewModel i have the next property:
public IEnumerable<LocalizationModel> LocalizationList
{
get => _localizationList;
set => Set(ref _localizationList, value);
}
In that way i try to hold information about key and local value.
On my View i want to show this collection using ListView or DataGrid with the possibility to change localization value. I need some approach to build columns dynamically using XAML, for example:
|Key |English |Denmark |Spanish|...
XAML is not a programming language. It is a markup language that has no support to add columns dynamically.
You could either add columns to the DataGrid programmatically, in the code-behind of the view, or you could set or bind the ItemsSource to a DataTable to which you can add columns dynamically.
I have a TypeOfContact model that is made up of an ID, and Text. For example, one type would be Telephone and the ID would be 1. Another type would be Email and the ID 2.
What I would like to do is add the text of the TypeOfContact as an item and the ID as a tag. I imagine it would look something like this, however this isn't working;
contactTypeComboBox.Items.Clear();
foreach (TypeOfContact c in ContactTypes)
{
contactTypeComboBox.Items.Add(c.ContactTypeText);
foreach (ComboBoxItem item in contactTypeComboBox.Items)
{
item.Tag = c.ContactTypeID;
}
}
The reason I want to do this is that when someone selects one of the ComboBox items I want to store the text and the ID. I could do this all through XAML but ContactTypes is a list that is populated by the user, so I cannot hard code the values into the ComboBox as maintaining it and adding new TypesOfContact would be difficult.
I fixed this issue myself by first adding;
DisplayMemberPath="ContactTypeText" SelectedValuePath="ContactTypeID"
to the XAML of the ComboBox then accessing the ID like;
contactTypeComboBox.SelectedValue
In your situation i would bind the list of your TypeOfContacts as ItemsSource to the ComboBox. After that you could set the tag, but i think you don't will need it, because when you also bind the SelectedItem you got back the whole item (ID, type, ...) and can work with it in other parts of your code.
Example for simplifying without a ViewModel (but you should use one):
Codebehind
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = this;
FillListWithSomeExamples();
}
private void FillListWithSomeExamples()
{
TypesOfContacts.Add(new TypesOfContact {Id = 1, Type = "Email"});
TypesOfContacts.Add(new TypesOfContact { Id = 2, Type = "Telephone" });
}
public TypesOfContact SelectedTypesOfContact { get; set; }
public ObservableCollection<TypesOfContact> TypesOfContacts { get; set; } = new ObservableCollection<TypesOfContact>();
}
TheTestmodel:
public class TypesOfContact
{
public int Id { get; set; }
public string Type { get; set; }
}
XAML
<Grid>
<ComboBox ItemsSource="{Binding TypesOfContacts}" SelectedItem="{Binding SelectedTypesOfContact}" DisplayMemberPath="Type"/>
</Grid>
Now you can read the selected item in any other method of the MainWindow by looking at SelectedTypesOfContact.
I'm currently Binding my Listbox to a DTO. Following MVVM patterns, How do I interact with what was selected from the Listbox.
I want something like this [1 being the "FileName"
If (Listbox.Selecteditem[1] == "samplefilename")
{ Messagebox.Show("Files matched"}
But how exactly is that done using MVVM? Do I have to create SelectedValue bindings/properties?
public class FilesDTO : IDTO
{
public int Id { get; set; }
public string FileName { get; set; }
public string FileExtension { get; set; }
public byte[] FileArray { get; set; }
}
You just need to add a "SelectedFileDTO" to your ViewModel and in your XAML, make sure it's set to TwoWay. When it changes in the View, your SelectedFileDTO setter will be hit in your ViewModel.
<ListBox ItemsSource={Binding Files} SelectedItem={Binding SelectedFileDTO, Mode=TwoWay}/>
public FilesDTO SelectedFileDTO
{
get...
set...
}
Yes, you should bind the SelectedValue property to a DependencyProperty. Then whenever you want to use it, it is right there at your disposal. You could also give the DependencyProperty changed events to utilize.
Is there a way to access the value of a dynamic field from another dynamic field template?
For example let's say that in my model I have something like this:
//auto generated by my data context
partial class Foo
{
public string Name { get; set; }
public bool ShouldGenerateNameDynamically { get; set ; }
}
[MetadataType(typeof(FooMetadata))]
partial class Foo{}
class FooMetadata
{
[UIHint("DynamicName")]
public object Name { get; set; }
}
So based on the value of ShouldGenerateNameDynamically I'd like to enable or disable and assign a calculated value to the Name field in my custom DynamicName template either in the PreRender or OnDataBinding events
So is there any way to send like an event across the dynamic data templates or access another field from a field template?
If having a List filled with objects of the following type:
public class Person {
public string Name { get; set; }
public string Ssn { get; set; }
}
How can one set the value of the property Name to be what's beeing displayed within an ASP.NET DropDown, without setting any properties on the ASP.NET DropDown object? Of course, one way would be to ovveride ToString() on the class Person, but is there any other way to accomplish the same thing?
Thanx!
You can create new control derived from DropDown (e.g. DropDownEx), introduce new attributes e.g. DisplayFieldAttribute, ValueFieldAttribute. Then use TypeDescriptor.GetProperties to enumerate properties and analyze attributes for binding them respectively.