How to load ItemsSource before setting SelectedItem in ListView? - c#

I have a MVVM page that contains a ListView. I bind ItemSource and SelectedValue, but first time it calls converter for SelectedValue then loads ItemSource.
<ListView x:Name="ListViewSurahs"
ItemsSource="{Binding MyItems}"
FlowDirection="LeftToRight"
Grid.Column="2"
Grid.Row="4"
VerticalAlignment="Top"
HorizontalAlignment="Left"
HorizontalContentAlignment="Center"
SelectionMode="Single"
ScrollViewer.VerticalScrollBarVisibility="Auto"
DisplayMemberPath="Name"
SelectedValuePath="ID"
SelectedValue="{Binding Source={StaticResource CurrentInfo},
Path=Instance.ID,Mode=OneWay}"
ShowsScrollingPlaceholders="False" />
because of that I lose SelectedItem and no Items get selected. what should I do to load ItemsSource first?

Firstly you can try to type your ItemsSource in codebehind. You have to add an UserLoaded property in your xaml file to do this. But maybe we need to see your codebehind and viewmodel. Anyway you should try to change your SelectedValue binding mode OneWay to the TwoWay.
After that you should watch your binding style. You have to complete most of your development progress in your viewmodel, and after that you could just call your viewmodel from your xaml codebehind (.cs) with get-set. So, you will have a very clean binding structure.
In this way you can type as follows instead of yours,
SelectedValue="{Binding Model.BlaBla, Mode=TwoWay}"
in here Model is defined and called in your codebehind of xaml file (.cs). For example in top of your public sealed partial class
public YourViewModelName Model { get; set; }
and in the same file public YourXamlName()
Model = new YourViewModelName();
It is a quickly answer and I am not sure. But you should give a shot.
Good luck.

Related

How to bind dependency property to datacontext of datatemplate in silverlight without throwing error?

I've been having some trouble in my silverlight app, and am hoping one of you may help me. :) I am attempting to set up a datatemplate for a datagrid but am having some trouble with a binding. It seems that when I set my binding mode to "TwoWay", I get the following error:
Note that I am trying to bind to the DataContext that is automatically "assigned" to the datatemplate
Provide value on 'System.Windows.Data.Binding' threw an exception.
Can anyone provide me with a solution to this problem? My objective is to have the datatemplate in a separate view to allow greater customization to my datagrid. The following is a dumbed down version of what I am attemping:
<sdk:DataGrid ItemsSource="{Binding items}" SelectedItem="{Binding selItem, Mode=TwoWay}">
<sdk:DataGrid.Columns>
<sdk:DataGridTemplateColumn Width="*">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<View:leadDataTemplate item="{Binding Mode=TwoWay}"/>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
</sdk:DataGrid.Columns>
</sdk:DataGrid>
Where <View:leadDataTemplate item="{Binding Mode=TwoWay}"/> would be the view I am attempting to initialize.
Hopefully you get the idea, thanks in advance for any suggestions or answers!
Edit:
Note that item is the dependency property, and it doesn't matter if the source is updated when changes are made to the item, I simply need the datagrid to display information within the item, so I require the dependency property to be set, thanks!
Second edit:
I believe the problem may be that my Binding isn't passing any data at all to the view, do you know why this would be? I know that the items section is being filled, so why would the binding be empty? Also, please note that the dependency property is supposed to update another viewmodel as follows:
public static readonly DependencyProperty itemProperty = DependencyProperty.Register("item", typeof(object), typeof(leadDataTemplate), new PropertyMetadata(new PropertyChangedCallback(itemChanged)));
public leadStreamData item
{
get { return (object)GetValue(itemProperty); }
set { SetValue(itemProperty, value); }
}
private static void itemChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var ctrl = (leadDataTemplate)d;
if (ctrl != null)
ctrl._vm.item = ctrl.item;
}
TwoWay binding is only required when you wish to update the source data.
The following should suffice:
<sdk:DataGrid ItemsSource="{Binding items}" SelectedItem="{Binding selItem, Mode=TwoWay}">
<sdk:DataGrid.Columns>
<sdk:DataGridTemplateColumn Width="*">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<View:leadDataTemplate item="{Binding .}"/>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
</sdk:DataGrid.Columns>
</sdk:DataGrid>
To use TwoWay binding, you must provide a Path property e.g.
<TextBox Text="{Binding FirstName, Mode=TwoWay}" />

How to Bind with Combobox in silverlight

I want to bind my collection of countries to a Combobox.
My XAML page looks as follows:
<pmControls:pmComboBox
Margin="3" Grid.Row="5" Grid.Column="1"
ItemsSource="{Binding Path=Countries}"
SelectedValue="{Binding Title,Mode=TwoWay}" >
</pmControls:pmComboBox>
I want to display Title as DataTextField.
Currently it shows namespace of country class in my combobox list .
I have also tried to add DisplayMemberPath but it also not works.
How can I setup the display field of Combobox to use a Binding?
I think you need to do
DisplayMemberPath="Title"
SelectedValuePath="Title"
SelectedValue="{Binding Path=Country,Mode=TwoWay}"

Combobox not rendering in wpf datafrid

I have my data grid "dgSubsytem" column defined like below
<my:DataGridComboBoxColumn x:Name="cmbSubSysSupplier_SRV" Header="Supplier" Width="160"
ItemsSource="{Binding RelativeSource}" SelectedValueBinding="{Binding SupplierId}" />
As you see from the code i am having a combo box inside a grid .
Item source of this combo box is a datatable which is bound to it in the code behind .
Item source of the grid also another datatable bound in code behind .
code of binding item source of combobox in code behind is as follows
cmbSubSysSupplier_SRV.ItemsSource = dsComboBox.Tables[3].DefaultView;
cmbSubSysSupplier_SRV.DisplayMemberPath="FullName" ;
cmbSubSysSupplier_SRV.SelectedValuePath = "SupplierId";
Problem is combo box itself not rendering . But I can see the value of the Supplier rendered as text . What is the problem?
There are 2 parts here:
List of values to be populated in ComboBox: ItemsSource, should be bound using a StaticResource, with a List<X> fields exposed from your ViewModel.
The actual value (here X) should be bound to SelectedItemBinding using binding to data item.
No binding in code behind required.
At what point does your code-behind stuff run?
You're setting the ItemsSource in two places - in the XAML and in the Code-Behind. Whichever one runs second will overwrite the value of the first one, so only the last value set will be used.
I suspect your XAML is getting run last, and RelativeSource is probably not a property on your DataContext, so your ComboBox ends up being bound to nothing.
To fix it, simply remove your ItemsSource binding in the XAML for the DataGridComboBoxColumn
<my:DataGridComboBoxColumn x:Name="cmbSubSysSupplier_SRV"
Header="Supplier" Width="160"
SelectedValueBinding="{Binding SupplierId}" />
In addition, the DefaultView of a DataTable will return an object of type DataView, and DataView does not have properties called FullName or SupplierId, so your SelectedValuePath and DisplayMemberPath properties won't work.
I'd recommend building a list of KeyValuePair<int,string> out of your data items, and bind your ComboBoxColumn.ItemsSource to that list, then switch the SelectedValuePath to "Key" and the DisplayMemberPath to "Value"
I personally fought with DataGridComboBoxColumn for long time and i think the way is to use DataGridTemplateColumn. Here is an exemple :
Looks a lot of code but evective.
Put the collection as resource :
<Grid.Resources>
<CollectionViewSource x:Key="StructuresCollection" Source="{Binding StructuresList, Mode=OneTime}"/>
</Grid.Resources>
<DataGridTemplateColumn Header="Structure" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Structures.Name}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate >
<DataTemplate>
<ComboBox x:Name="CStructures" SelectedItem="{Binding Structures}" DisplayMemberPath="Name" SelectedValue="{Binding IDStructure, UpdateSourceTrigger=PropertyChanged}" SelectedValuePath="{Binding IDStructure}" ItemsSource="{Binding Source={StaticResource StructuresCollection}}" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>

Bind button in DataTemplate to command in the form's ViewModel

My problem is similar to the one described in this question:
WPF MVVM Button Control Binding in DataTemplate
Here is my XAML:
<Window x:Class="MissileSharp.Launcher.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MissileSharp Launcher" Height="350" Width="525">
<Grid>
<!-- when I put the button here (outside the list), the binding works -->
<!--<Button Content="test" Command="{Binding Path=FireCommand}" />-->
<ListBox ItemsSource="{Binding CommandSets}">
<ListBox.ItemTemplate>
<DataTemplate>
<!-- I need the button here (inside the list), and here the binding does NOT work -->
<Button Content="{Binding}" Command="{Binding Path=FireCommand}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Window>
It's just a ListBox, bound to an ObservableCollection<string> named CommandSets (which is in the ViewModel).
This binding works (it displays a button for each item in the collection).
Now I want to bind the button to a command (FireCommand), which is also in the ViewModel.
Here's the relevant part of the ViewModel:
public class MainWindowViewModel : INotifyPropertyChanged
{
public ICommand FireCommand { get; set; }
public ObservableCollection<string> CommandSets { get; set; }
public MainWindowViewModel()
{
this.FireCommand = new RelayCommand(new Action<object>(this.FireMissile));
}
private void FireMissile(Object obj)
{
System.Windows.MessageBox.Show("fire");
}
}
The binding of this button does NOT work.
From what I've understood from the question I linked above, the binding doesn't work because:
(correct me if I'm wrong)
The button is inside the ListBox, so it only "knows" the binding of the ListBox (the ObservableCollection, in this case), but not the binding of the main window
I'm trying to bind to a command in the main ViewModel of the main window (which the button doesn't "know")
The command itself is definitely correct, because when I put the button outside the ListBox (see the XAML above for an example), the binding works and the command is executed.
Apparently, I "just" need to tell the button to bind to the main ViewModel of the form.
But I'm not able to figure out the right XAML syntax.
I tried several approaches that I found after some googling, but none of them worked for me:
<Button Content="{Binding}" Command="{Binding RelativeSource={RelativeSource Window}, Path=DataContext.FireCommand}" />
<Button Content="{Binding}" Command="{Binding Path=FireCommand, Source={StaticResource MainWindow}}" />
<Button Content="{Binding}" Command="{Binding Path=FireCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" />
Could someone please:
give me the proper XAML to bind the button inside the ListBox to a command in the form's MainViewModel?
point me to a link where this advanced binding stuff is explained in a way that a WPF/MVVM beginner can understand?
I'm feeling like I'm just copying and pasting arcane XAML incantations, and so far I don't have any clue (and can't find any good documentation) how I would figure out by myself in which cases I'd need RelativeSource or StaticResource or whatever instead of a "normal" binding.
It's:
{Binding DataContext.FireCommand,
RelativeSource={RelativeSource AncestorType=ListBox}}
No need to walk up to the root unless you actually change the DataContext along the way, but as the ListBox seems to bind to a property on the main VM this should be enough.
The only thing i recommend reading is the Data Binding Overview, and the Binding class documentation (including its properties).
Also here is a short explanation on how bindings are constructed: A binding consists of a source and a Path relative to that source, by default the source is the current DataContext. Sources that can be set explicitly are: Source, ElementName & RelativeSource. Setting any of those will override the DataContext as source.
So if you use a source like RelativeSource and want to access something in the DataContext on that level the DataContext needs to appear in the Path.
This may be considered unrelated by most, but this search is only 1 of 3 results that you'll find searching for data binding commands to controls inside a data template--as it relates to Xamarin Forms. So, maybe it'll help someone now-a-days.
Like me you may wonder how to bind commands inside a BindableLayout. Credit jesulink2514 for answering this at Xamarin Forums, where it's probably overlooked by many because of all the comments. Here's his solution, but I'm including the link below:
<ContenPage x:Name="MainPage">
<ListView Grid.Row="1"
ItemsSource="{Binding Customers}"
VerticalOptions="Fill"
x:Name="ListviewCustomer">
<ListView.ItemTemplate>
<DataTemplate>
<Label Text="{Binding Property}"/>
<Button Command="{Binding BindingContext.ItemCommand, Source={x:Reference MainPage}}"
CommandParameter="{Binding .}">Click me</Button>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage>
https://forums.xamarin.com/discussion/comment/217355/#Comment_217355

Bind ObservableCollection<XmlNode> to combobox wpf

I'm trying to bind a combobox to a ObservableCollection.When the form is displayed the combobox is empty.The same code with ObservableCollection of type string works perfectly. I've got a feeling that my XPath is wrong. Any suggestions are welcome:
XAML:
<ComboBox ItemsSource="{Binding ItemParameters, XPath=InnerXml/name,Mode=TwoWay}" SelectedIndex="0" Margin="2" VerticalAlignment="Top" HorizontalContentAlignment="Stretch" Grid.Row="1" Grid.Column="1" Height="24" />
ObservableCollection XmlNode :
public ObservableCollection<XmlNode> _itemParameters = new ObservableCollection<XmlNode>();
public ObservableCollection<XmlNode> ItemParameters
{
get { return _itemParameters; }
set { _itemParameters = value; }
}
The combobox should display the name attribute of each XmlNode in the collection:
Update:
I've tried using DisplayMemberPath in two different ways, but the combobox still contains no data:
DisplayMemberPath="{Binding XPath=name}" ItemsSource="{Binding ItemParameters}"
DisplayMemberPath="{Binding XPath=InnerXml/name}" ItemsSource="{Binding ItemParameters}"
Solution:
This did the trick, hope it helps someone else as well:
<ComboBox DisplayMemberPath="#name" ItemsSource="{Binding ItemParameters}"
First of all you are setting Path and XPath at the same time, which are conficting properties, secondly you bind the ItemsSource, which has nothing to do with what you want to show inside the item. Either use DisplayMemberPath or an ItemTemplate for that, the ItemsSource should just be bound to ItemParameters.

Categories