I have observed a strange behavior, I have a XAML Combobox (SilverLight4) declaration with bound SelectedItem and ItemsSource and it appears that the order of SelectedItem and ItemsSource does matter... If SelectedItem is 2nd the control doesn't init properly (nothing is selected), if it was code I can understand why it matter because you can't select item which is not in combobox items collection, but this is XAML...
Do I understand it right that XAML attributes order influences generated code line order? Or it's just something wrong with my code?
That's right - check the answers here: Silverlight XAML Attribute Definition Order Matters, it's due to the XAML being used to create the objects.
Related
Okay so in wpf xaml markup the data binding features allow for data binding a list to a listView. To do I specified the DataContext and set the ItemSource to the DataContext. This works and the listView is populated with the items. The items though are objects and to be able to have the displayed item in the list view as a string property of get set implementation from the object enumerated in the list view I am attempting to use an Item Template. Though this is not working and I'm uncertain why.
How exactly does Item Template and the Data Binding function in this scenario? And what would be workable solutions to this problem? Is there a way to specify the property in the text box as being local to the enumerated objects. Please Help I've honestly been working on this for a while and research hasn't provided the answers.
ItemList is a list of objects. ItemName is a string property of ItemList. The ItemSource and DataContext works but the textblock does not.
Preferably a solution using only xaml.
<ListView DataContext="this.ItemList" ItemsSource="{Binding }" Name ="Items">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding ItemName}" >
</TextBlock>
</DataTemplate>
</ListView.ItemTemplate>
Except for some unconventional ways of working pointed out by Clemens, I think the only actual error you make is the fact that ItemName is a property of ItemList instead of the enumerated objects' class.
You ask "Is there a way to specify the property in the text box as being local to the enumerated objects". Well, this is the actual case: the TextBlock's datacontext IS the enumerated object. More precisely: all bindings in the itemtemplate will go look in the enumerated object unless the binding explicitly tells otherwise. So, if your enumerated object has eg. a string property ObjectName, its value will definitely get bound and shown by the textblock.
I've spent hours trying to figure out what is happening to my selecteditem property of my ComboBoxes and Im pretty sure I know what is causing the problem now, but I just dont understand why this is happening and if there is something I can do about it.
Basically I have a DataGrid with comboboxes in the headers that I use for filtering the DataGrid items. The DataGrid sits in a Tabcontrol and if I select items in the comboxes and switch tab and back the selected items are removed.
This happens only when the ItemsSource of the Comboboxes have the IsAsync property set to True. Otherwise everything works just like I want it to.
To give some more info about the logic:
The comboboxes get their values from a IValueConverter its the same converter for all comboboxes and I pass a ConverterParameter to tell it what to return. The collection sent to the converter is the items shown in the datagrid.
These values are refreshed (each item has selected parameters updated, the collection is not cleared and recreated) when the tab is activated.
This my xaml for one of the comboboxes:
<ComboBox SelectedItem="{Binding MadeBy}" ItemsSource="{Binding IssuesView,Converter={StaticResource DataGridFilterableValueConverter}",ConverterParameter=Madeby, IsAsync=true}" SelectionChanged="FilterComboBox_SelectionChanged"/>
Now, like i described previously, if I set the IsAsync to false the selecteditems stay.
Do I just have to live with this? We work alot with sql-databases and IsAsync really makes the interface alot more snappy. I can live without it in the comboboxes, but I tend to use it as much as I can.
I've seen that ppl have been having problems with the selecteditem and the general solution seems to be placing the SelectedItem before the ItemsSource, but this does not help.
Thanks
Erik
Edit 1:
Here is the xaml of the tabcontrol and tabitem.
<TabControl Grid.Row="1" x:Name="TabControl" SelectionChanged="TabControl_SelectionChanged">
<TabItem Header="Granskningssynpunkter" x:Name="IssueTab">
..... Lots of code here but nothing that concerns TabControl or TabItem
I was wandering if someone can explain to me how the Dependency Property DisplayMemberPath works?
I am trying to create Custom ItemsControl that has property like DisplayMemberPath of a ComboBox, in otherwords after setting the ItemsSource I want to be able to specify the Property to Display.
At the moment if I do somthing like:
<cc:MyControl ... DisplayMemberPath="MyObjectDescription" ... >
(Yes I have overridden the DisplayMemberPath, its besides the point).
It displays a list of items, but they each Display "MyObjectDescription", instead of the value that that Property holds for each object in the ItemsSource.
And I believe its because I am missing something in regards to how DisplayMemberPath Property works.
Thanks All. :)
There are two types of DisplayMemberPath. One that supports Binding and one where you have to set a string value. In your case as I can see you wish to implement the second one. To do so create a property inside your custom control of type string and name it DisplayMemberPath. Override the methode OnInitialized in your container with your custom logic where you tell the container to manipulate the path of the binding to DataContext by changing binding's path to the string value as you specified in DisplayMemeberPath. WPF calls OnInitalized once any control is completely initalized but before its about to get rendered. I hope this helps you any futher.
I'm assuming your control is like MyControl and MyControlItem like ListBox and ListBoxItem.
You can access the DisplayMemberPath of MyControl when the MyControlItem is being created and use it to get the data from the DataContext.
Bit late to party, but maybe other could be helped
If your purpose is barely to use Itemscontrol over ListBox/View, you may consider to define the Datatemplate for the itemscontrol's Items instead of packing this in a Usercontrol:
<ItemsControl ItemsSource="{Binding myObjectCollection}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding myObjectProp}"/> (or whatever...)
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
I may be making this more complicated than necessary ... but here goes.
I have my MainPage.xaml, in there I have references to two other UserControl's, ResultsView, and DetailsView.
Inside of the ResultsView, I have a ListBox bound to an ObservableCollection of custom items, I have a DataTemplate that is rendering each item. The item has a CaseID, and when I click on it, it's displayed as a HyperlinkButton, I need a Command I've set in the MainPageViewModel to fire, which handles changing the visibility to hide the ResultsView, and show the DetailsView.
How do I bind the Command of the Hyperlinkbutton to the Command located in my MainPageViewModel?
Thanks in advance!
edit for clarification:
MainPage.xaml
<resultsView:ResultsView/>
<detailsView:DetailsView/>
Each of the two views (ResultsView & DetailsView) have their own ViewModel. So I'm going from my DataTemplate which resides in a ListBox inside my ResultsView, I need to go up to the MainPageViewModel, an extra step than your answer mentioned. Tried your method naming my MainPage.xaml to Name="mainPage", and using that as the ElementName in my HyperlinkButton, but no luck. I'll do some research on the RelativeSource option and see if I can make that work.
Thanks for your help so far.
edit 2: Forgot to add that the DataTemplate is in a ResourceDictionary, not in the ResultsView.
Well, it depends on the specific details, not all of which you've told us, but I can give you some examples.
Let's say that your DataTemplate for the custom items resides in the XAML of the ResultsView user control. That's a good spot for it but you might have put it in a resource dictionary.
As you say, the ListBox is bound to the collection of custom items. Let's further assume that the custom items collection is itself a property in your MainPageViewModel. And you've said clearly that the command you want to bind to is also in MainPageViewModel.
So the problem, which is a very common one, is that you are working inside a template associated with a binding to a collection, and so your DataContext is a custom item. It is no longer the main view-model itself. That's great as you show appropriate properties of the custom item like CaseID. But it's not great when you want to escape to the top-level of view-model.
If what I've said is true, then the ResultsView user control is in fact probably bound to the MainPageViewModel because you haven't "drilled into" the custom items collection yet. So, what you need to do is find a way using the binding syntax to reference the ResultsView user control from inside the DataTemplate for the ListBox. If you can do that, then you've escaped the collection.
There are two main approaches to do this:
ElementName syntax
RelativeSource syntax
I'll describe ElementName syntax and you can look up the other one.
Part 1) Name your ResultsView UserControl element like this:
<UserControl ....
Name="resultsView">
<!-- ... -->
Part 2) Inside your DataTemplate where you are defining the appearance of the hyperlink use the ElementName syntax to refer to that element:
<TextBlock>
<Hyperlink Command="{Binding DataContext.ItemDetailsCommand, ElementName=resultsView}"/>
</TextBlock>
So first we use ElementName to get the ResultsView UserControl element, and then we have a path with two pieces: the first piece is the DataContext property of the ResultsView which gives us the MainPageViewModel (yeah!), and then the property of the command we want to invoke.
That's one way to "escape" the binding and issue commands found at a higher level in the view-model.
Duplicate of :
Why are DataContext and ItemsSource not redundant?
In WPF we can assign list of item to ComboBox in 2 ways
//CODE #1
//WPF
<ComboBox name="cmbItems" ItemSource={Binding} />
//C#
cmbItems.DataContext = someList;
another way, directly assign itemsource
//CODE #2
//WPF
<ComboBox name="cmbItems" ItemSource={Binding} />
//C#
cmbItems. ItemSource = someList;
both serves the purpose, but whats the difference in above snippet? and which is good to use?
DataContext is mostly used on forms, controls etc.
An ItemSource is a relative path to do databinding on that DataContext.
For example when you make a form to edit Person details, then the DataContext would be Person and the different controls on the form will each bind on a seperate property on that object, for example Name, Date of Birth, etc.
In the second example you can leave out the ItemsSource={Binding}.. You are setting the ItemsSource directly to a value in your code behind.. You won't need a binding here. In your first example, you set the DataContext, and use a binding to retrieve it again from the DataContext..
But it doesn't really matter.. for both methods work fine...
I use the following thumb of rule: set it in code behind, if I have the collection available.. Set it in some kind of binding mode, if I need to transform the collection, so that I can use a IValueConverter to do the job...