windows 10 listitem with checkbox - windows-phone

i have one Restfulservice. when i call that service it gives the items with checkbox. after display the items. user selects some items and click on save. i have to send that information to db via the restful services.
can u please tell..
1) how to display the items from webservice [columns and rows] .
itemname selected or unselected
item1 []
item2 []
item3 []
2) how to send the selected or unselected items back to db via restful services.

1) XAML Example:
<ItemsControl ItemsSource="{Binding ItemsList}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<RadioButton
DataContext="{Binding }"
Content="{Binding Label}"
IsChecked="{Binding IsChecked}"
x:Name="radioButton"
GroupName="SomeGroupName"
/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
DTO:
ListItem is a list for items XD.
Items its like:
Public class items
{
public string label {get ; set;}
public bool IsChecked {get; set;}
}
2)Use Json for send and recive data.

Related

Avalonia TreeView Template Selector

I've had a go at using a template selector with a TreeView (see this project: https://github.com/imekon/AvaloniaTreeViewTemplate)
It creates a tree but the node no longer has an arrow to allow me to see the child nodes.
This is the XAML I used:
<TreeView Items="{Binding Things}">
<TreeView.Items>
<scg:List x:TypeArguments="vm:ThingViewModel">
<vm:ThingViewModel Template="Folder"/>
<vm:ThingViewModel Template="Thing"/>
</scg:List>
</TreeView.Items>
<TreeView.DataTemplates>
<helpers:ThingTemplateSelector>
<TreeDataTemplate x:Key="Folder" ItemsSource="{Binding Children}">
<TextBlock Text="{Binding Name}"/>
</TreeDataTemplate>
<TreeDataTemplate x:Key="Thing">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}"/>
<TextBlock Text="{Binding Address}"/>
</StackPanel>
</TreeDataTemplate>
</helpers:ThingTemplateSelector>
</TreeView.DataTemplates>
</TreeView>
and this is the selector:
public class ThingTemplateSelector : IDataTemplate
{
public bool SupportsRecycling => false;
[Content]
public Dictionary<string, IDataTemplate> Templates { get; } = new Dictionary<string, IDataTemplate>();
public IControl Build(object data)
{
return Templates[((ThingViewModel)data).Template].Build(data);
}
public bool Match(object data)
{
return data is ThingViewModel;
}
}
I can get a tree view working if I use a simple TreeDataTemplate but then I can't get it to select the correct template for the type of entry (a 'thing' or a 'folder'), i.e. all the nodes are one type.
Is this possible with Avalonia right now?
The picture above shows the 'Thing' node but no arrow next to it. Click on it simply selects the item but you can't expand it to see the child node.

uwp twoway binding not updating UI

I have the following XAML code, where I have a TextBox for user input (phone number) and GridView for user contacts.
<GridView x:Name="ContactsCollection" Grid.Row="1" Style="{StaticResource DefaultGridView}" IsItemClickEnabled="True" ItemsSource="{Binding UserContacts}" ItemTemplate="{StaticResource HorizontalGridViewDataTemplate}" ItemContainerStyle="{StaticResource DefaultGridViewItemContainer}">
<i:Interaction.Behaviors>
<c:EventTriggerBehavior EventName="SelectionChanged">
<c:InvokeCommandAction Command="{Binding SelectContactCommand}" CommandParameter="{Binding SelectedItem, ElementName=ContactsCollection}"/>
</c:EventTriggerBehavior>
</i:Interaction.Behaviors>
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</GridView.ItemsPanel>
And my TextBox that is binded to the Recipient in the ViewModel.
<TextBox x:Uid="Recipient" Text="{Binding Recipient, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" PreventKeyboardDisplayOnProgrammaticFocus="True" toolkitControls:TextBoxMask.Mask="7(999)999-99-99" toolkitControls:TextBoxMask.PlaceHolder="X" InputScope="NumericPin" HeaderTemplate="{StaticResource UiControlHeaderDataTemplate}" Style="{StaticResource DefaultTextBoxStyle}"/>
Here goes code from the ViewModel.
public string Recipient
{
get { return _recipient; }
set
{
if (value != _recipient)
{
_recipient = value;
RaisePropertyChanged(nameof(Recipient));
ContinueTransferCommand.RaiseCanExecuteChanged();
}
}
}
Now, when I click any contact from the GridView the following functions is called, that assign contact's phone number to the Recipient property.
private void SelectContactCommandAction(object contact)
{
Contact c = contact as Contact;
if (c.Phones?.Count > 0)
Recipient = FormatDataHelper.FormatPhoneNumber(c.Phones.FirstOrDefault().Number);
}
When I click any contact for the FIRST time, the Recipient property is updated, and the UI is updated too. But, if I select other contact, in the debug mode I can see, that Recipient property is updated and saved new value, but the UI (TextBox) is not updated at all. The only way it works, I have to delete any character from the TextBox and select a contact again, in that case UI value will be updated again.
Can somebody please explain this?

Bind CommandParameter to SelectedItem of GridView

I work an a Windows 8 application which shows a GridView on one page. When ever the user selects an item of this grid and clicks on a button, the next page is loaded with detail information of the selected item.
I am using MVVM for this and have a DelegateCommand from Prims:
public DelegateCommand<Route> ShowRouteDetailsCommand { get; private set; }
This command is initialized inside the constructor:
this.ShowRouteDetailsCommand = new DelegateCommand<Route>(this.ShowRouteDetails);
The navigation is done by Prisms navigation service:
private void ShowRouteDetails(Route route)
{
this.NavigationService.Navigate(PageNames.RouteDetails, route.Id);
}
The routes are shown inside a GridView:
<GridView x:Name="RouteGrid"
ItemsSource="{Binding Routes}"
SelectionMode="Single">
<GridView.ItemTemplate>
<DataTemplate> ...
The command is currently added inside the app bar (just for testing):
<AppBarButton Command="{Binding ShowRouteDetailsCommand}"
CommandParameter="{Binding SelectedValue,
ElementName=RouteGrid, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Icon="Forward" />
My problem is, that the parameter of ShowRouteDetails is allways empty. It doesn't even matter if I try GridViews SelectedValue or SelectedItem property.
I know that I could easily add a SelectedRoute property, bind the SelectedItem to it and use it in ShowRouteDetails but this seems dirty to me.
Why don't you just create a var in your viewModel and bind it to the SelectedItem of the gridView? In this way, when you run the command, you have only to read the value of that var.
<GridView x:Name="RouteGrid" ItemsSource="{Binding Routes}"
SelectionMode="Single" SelectedItem="{Binding myVar}">
<GridView.ItemTemplate>
<DataTemplate>

Silverlight listbox doesn't refresh list

I'm trying to load a listbox with a list of items being pulled from an OData web service. The data fetch is working well and I'm getting the list of items. The listbox actually works and displays the items, just not every time I start the application... Due to the requirement for the data to be pulled asynchronously sometimes the listbox loads up before the data has returned. When it does I feed it a list with a single 'empty' item to indicate that the data is still loading. Seconds later the data has loaded and I raise a PropertyChanged event for the list. My breakpoint in the list property triggers and when I check the list contains the correct items. But the listbox doesn't display the new items, only the old 'empty' item. It seems exceptionally odd to me that the xaml is clearly requesting the list but then doesn't refresh the layout for the new items.
First the code initialising the ViewModel. ModelReferenceMap implements INotifyPropertyChanged and so should be updating the view when OnPropertyChanged("Areas"); is called (this triggers a fetch of the list from the property but doesn't update the view).
public ModelReferenceMap(Uri serviceURI)
{
// Try initialising these lists to a non null but empty list
// in the hope it will stop the lists breaking when the service
// is a little bit slow...
areas = new List<ModelReferenceItem> { new ModelReferenceItem(null) };
// This is a ServiceReference entity context which will retrieve the data from the OData service
context = new LiveEntities(serviceURI);
// SendingRequest adds credentials for the web service
context.SendingRequest += context_SendingRequest;
// The query to retrieve the items
var areaQuery = from i in context.MigrationItems where i.FusionPTID == 0 && i.Type == "AreaType" orderby i.Name select i;
// On completion this asynccallback is called
AsyncCallback ac = iasyncResult =>
{
// Populates the List with the data items
areas = (from i in ((DataServiceQuery<MigrationItem>) areaQuery).EndExecute(iasyncResult)
select new ModelReferenceItem(i)).ToList();
foreach (ModelReferenceItem area in areas)
{
if (selectedArea == null)
selectedArea = area;
area.PropertyChanged += referenceItem_PropertyChanged;
}
// The Xaml Listbox has its ItemsSource bound to the Areas property. This should trigger a refresh of the listbox contents shouldn't it?
OnPropertyChanged("Areas");
OnPropertyChanged("SelectedArea");
};
// Start the query
((DataServiceQuery<MigrationItem>)areaQuery).BeginExecute(ac, null);
}
Now the XAML. Note that the DataContext of the listbox is ReferenceMap (a property on my main ViewModel which exposes a singleton instance of ModelReferenceMap). I've then bound the ItemsSource to Areas.
<ListBox Grid.Row="0" DataContext="{Binding ReferenceMap}" ItemsSource="{Binding Areas}" SelectedItem="{Binding SelectedArea, Mode=TwoWay}" HorizontalAlignment="Stretch" Margin="3" Name="listBoxFusionAreas" VerticalAlignment="Stretch">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Background="{Binding CompleteStatusColour}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBlock FontSize="12" Text="{Binding Name}" />
<TextBlock Grid.Column="1" FontSize="12" Text="{Binding Count}"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
The Areas property is triggering correctly indicating the binding is working. When Areas is only requested AFTER the service data has been retrieved (ie only once) the list works perfectly. If however the Areas property is triggered prior to the service data returning (ie with the single 'empty' item) it triggers again during the OnPropertyChanged("Areas"); call with the full set of items, only this time the list still just shows the original 'empty' item.
What am I doing wrong?
Whenever you are binding to a collection in your ViewModel you need to make sure whether the items in your collection are gonna change?? In your case you need to implement
ObservableCollection<ModelReferenceItem> areas ;
Instead of
List<ModelReferenceItem> area;
ObservableCollection implements INoifyCollectionChanged event that notifies your view about the changes in the collection (Add/Remove)

Why can't get validation error display in validationSummary?

I have a form with some validations set in entity metadata class. and then binding entity instance to UI by VM. Something as below:
Xaml like:
<Grid x:Name="LayoutRoot">
<StackPanel VerticalAlignment="Top">
<input:ValidationSummary />
</StackPanel>
<TextBox Text="{Binding Name, Mode=TwoWay}" />
<ComboBox x:Name="xTest" ItemsSource="{Binding MyList}"
SelectedItem="{Binding MyItem,Mode=TwoWay,
DisplayMemberPath="MyName"
ValidatesOnDataErrors=True,
ValidatesOnNotifyDataErrors=True,
ValidatesOnExceptions=True,
NotifyOnValidationError=True,UpdateSourceTrigger=Explicit}" />
</Grid>
Code-behind like:
public MyForm()
{
InitializeComponent();
this.xTest.BindingValidationError +=new EventHandler<ValidationErrorEventArgs>((s,e)=>{
BindingExpression be = this.xTest.GetBindingExpression(ComboBox.SelectedItemProperty);
be.UpdateSource();
if (e.Action == ValidationErrorEventAction.Added)
((ComboBox)s).Foreground = new SolidColorBrush(Colors.Red);
});
}
Metadata like:
[Required]
public string Name { get; set; }
[RequiredAttribute]
public int MyItemID { get; set; }
But when running the app, I got nothing display in valudationSummary.
For CombBox, even there is error, looks like BindingValidationError event is never fired.
How to resolve it?
Why are you using an Explicit UpdateSourceTrigger?
Silverlight validation happens inside the binding framework, when the binding is updating the source object. The way you have this, there won't be a binding validation error because you never tell the binding to update the source object. Well, actually you do, but it happens inside the validation error event handler. You've written chicken-and-egg code.
Remove your UpdateSourceTrigger on your binding or set it to Default.
Remove the explicit call to BindingExpression.UpdateSource.
Remove setting the ComboBox foreground to red - you are using NotifyOnValidationError=True, which eliminates any need to manually color the control.
Remove the DisplayMemberPath from the binding
So your XAML:
<Grid x:Name="LayoutRoot">
<StackPanel VerticalAlignment="Top">
<input:ValidationSummary />
<ComboBox x:Name="xTest" ItemsSource="{Binding MyList}"
SelectedItem="{Binding MyItem,
Mode=TwoWay,
ValidatesOnDataErrors=True,
ValidatesOnNotifyDataErrors=True,
ValidatesOnExceptions=True,
NotifyOnValidationError=True}" />
</StackPanel>
</Grid>
And your code:
public MyForm()
{
InitializeComponent();
// you don't need anything here to have the validations work
}