Hello i'm new in programming and i making a Football Windows Phone 8.1 Application using Web Services. i created a class Football.cs:
public int Id { get; set; }
public string Team1 { get; set; }
public string Team2 { get; set; }
public string Score { get; set; }
public string Place { get; set; }
public string Stadium { get; set; }
Then i generated a Web API 2 Controller using Entity Framework to create the DataBase and get the data from it.
Then i created a Windows Phone application, in the Mainpage.xaml here's the code :
<Page.Resources>
<DataTemplate x:Key="EmployeeDataTemplate">
<Grid Margin="12,12,12,0"
Width="376">
<TextBlock
FontSize="24"
Text="{Binding Team1}"/>
<TextBlock
FontSize="24"
HorizontalAlignment="Right"
Text="{Binding Team2}"/>
<StackPanel>
<TextBlock
FontSize="18.667"
HorizontalAlignment="Center"
Text="{Binding Score}" />
</StackPanel>
</Grid>
</DataTemplate>
</Page.Resources>
<Page.DataContext>
<Core:MainViewModel/>
</Page.DataContext>
<Grid>
<ListView x:Name="mainListView"
ItemsSource="{Binding EmployeesList}"
ItemTemplate="{StaticResource EmployeeDataTemplate}"
IsItemClickEnabled="True"
ItemClick="listView_ItemClick"/>
</Grid>
then i created a new BasicPage.xaml:
<Page.DataContext>
<Core:MainViewModel/>
</Page.DataContext>
<StackPanel>
<TextBlock Text="{Binding Place}" />
<TextBlock Text="{Binding Stadium}"/>
</StackPanel>
Can you help me please, i'm a beginner and i tried to make it but without success. All i want is when i click in a specific Item in the ListView, the Basic Page will display the Place and the Stadium to ONLY and ONLY to this specific Item, not the first one and not all of them. Thank you very much, i appreciate it
See my answer here, I use the Behaviours Sdk(how to add) in conjunction with a custom converter class to receive the tapped item in the list. I believe this is what you are looking for.
If you are using MVVM Light you can do this with generic RelayCommand<T> like this:
<ListView x:Name="mainListView"
ItemsSource="{Binding EmployeesList}"
ItemTemplate="{StaticResource EmployeeDataTemplate}"
IsItemClickEnabled="True"
Command={Binding ShowMathInfo, Source={StaticResource MainViewModel}}"
CommandParameter="{Binding }"/>
Then in your command execute method you will get item what you clicked on, then you will be able to pass this item to another page or what you need to do with it.
there's no propertie called Command under ListView.
or maybe it's not like this ? am i wrong ? sorry but i'm a really beginner
Related
I can't make the {Binding Title} in the HeaderTemplate appear.
This is the class connected to the BindingContext:
class SensorGroup
{
public string Title { get; set; }
public IList<Sensor> Sensors { get; set; }
}
XAML:
<ListView Header=""
ItemsSource="{Binding Sensors}">
<ListView.HeaderTemplate>
<DataTemplate>
<Grid>
<Label Text="{Binding Title}"/>
</Grid>
</DataTemplate>
</ListView.HeaderTemplate>
<ListView.ItemTemplate>
...
</ListView>
If I replace it with <Label Text="Some static text"/>, the text appears.
I have found this related question, which links to this other question. But I could not make it work. I tried:
<ContentPage.Resources>
<Label x:Key="MyTitle"
Binding="{Title}"/>
</ContentPage.Resources>
...
<Grid>
<StaticResource ResourceKey="MyTitle"/>
</Grid>
It gives me an error saying that the binding with Title cannot be found.
Sounds you like just need to do:
<ListView Header="{Binding .}"
ItemsSource="{Binding Sensors}">
That is if your ContentPage's BindingContext is set to the SensorGroup class.
The above is telling the ListView.Header to be bound to what ever the ContentPage.BindingContext is set to. That means that the ListView.HeaderTemplate controls will also use what ever ContentPage.BindingContext is set to.
Let me know if that does not make any sense.
I had to make the following combobox for a wpf application,
for the following type
public class Protocol
{
public int Id {get;set}
public string Name {get;set}
public int Speed {get;set}
public int Security {get;set}
}
So I made a custom Rating control which takes images and an Integer (i.e speed or security) to fill a rectangular WrapPanel with images.
I created the DataTemplate for the combobox as follows
<ComboBox x:Name="pro" IsSynchronizedWithCurrentItem="True" Grid.Row="1" Margin="40,41,50,0" VerticalAlignment="Top"
Style="{StaticResource ProtocolComboBoxStyle}"
ItemsSource="{Binding Protocols}"
SelectedItem="{Binding Protocol, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
Height="29" SelectionChanged="ComboBox_SelectionChanged">
<ComboBox.ItemTemplate>
<DataTemplate DataType="{x:Type models:Protocol}">
<WrapPanel Orientation="Horizontal">
<TextBlock Width="145" Padding="10,2,0,2" x:Name="text" Style="{StaticResource SettingsInactiveTextBlockStyle}" Text="{Binding Name}" HorizontalAlignment="Center"/>
<controls:RatingControl FilledImage="pack://application:,,,/SampleApp;component/Resources/Images/Settings/SettingsHome/security_filled.png"
UnfilledImage="pack://application:,,,/SampleApp;component/Resources/Images/Settings/SettingsHome/security_unfilled.png"
Rating="{Binding Security}" HorizontalAlignment="Right"/>
<controls:RatingControl Margin="5,2,0,0" FilledImage="pack://application:,,,/SampleApp;component/Resources/Images/Settings/SettingsHome/speed_filled.png"
UnfilledImage="pack://application:,,,/SampleApp;component/Resources/Images/Settings/SettingsHome/speed_unfilled.png"
Rating="{Binding Speed}" HorizontalAlignment="Right"/>
</WrapPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
It works fine except after the initial selection, only the name of the protocol is changing in the togglebutton, the rating controls on the left do not change.
Also to mention, Selecting an item does change the binded selectedProtocol property in the viewmodel.
Please tell me if I am missing anything?
Thanks in advance
I am using WPF + MVVM.
If I have a ComboBox, normally the names listed in the Dropdown are is defined by the .ToString() overload on the contents of the ObservableCollection.
Is there a way to control the names in the Dropdown list using XAML instead of the .ToString() overload?
What I have tried
I've spent a lot of time on this. The only solution I have come up with so far is to inherit from the original class, and define a new .ToString() override, but this solution is ugly, ugly, ugly.
You need to override the ItemTemplate.
E.g.
<ComboBox ItemsSource="{Binding MyItems}">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Rectangle Fill="{Binding Name}" Width="16" Height="16" Margin="0,2,5,2" />
<TextBlock Text="{Binding Name}" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
Or what you can do if you only want to display a specific field, is to use DisplayMemberPath
e.g.
<ComboBox ItemsSource="{Binding Path=PhonebookEntries}"
DisplayMemberPath="Name"
SelectedValuePath="Name"
SelectedValue="{Binding Path=PhonebookEntry}" />
You are binding to an ObservableCollection<T>. If you don't tell the binding engine what property to bind to in T, it will call .ToString() on the T class and place the results in a TextBlock.
Let's say you have a class, Trip
public class Trip
{
public string TripName { get; set; }
public string Description { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
}
To show the TripName in ComboBox, use a DataTemplate (as suggested by Michal). This allows you to specify what type of elements are used to render the bound data and what properties to bind to.
<ComboBox ItemsSource="{Binding Trips}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding TripName}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
To bind to a different property use another property name:
<TextBlock Text="{Binding Description }" />
In my XAML I have:
<Window.Resources>
<DataTemplate x:Key="CastTemplate">
<DockPanel Width="Auto">
<Image Source="{Binding image}" Width="10" Height="10"/>
<TextBlock Text="{Binding name}"/>
<TextBlock Text="{Binding character}"/>
</DockPanel>
</DataTemplate>
<Window.Resources>
<Grid HorizontalAlignment="Center">
<ItemsControl ItemTemplate="{StaticResource CastTemplate}" ItemsSource="{Binding SelectedItem.castInfoWithPics}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="3" Rows="2"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</Grid>
The binding points to a list of objects with the following properties:
public string name { get; set; }
public string character { get; set; }
public BitmapSource image { get; set; }
In my ViewModel, I assign the image property using:
cast.image = loadBitmap(bitmap);
I am using the loadBitmap function stated here: https://stackoverflow.com/a/1118557/2268507
The problem I'm having is that the image is not being displayed when I run my program. The name and character properties appear however.
I also don't seem to be receiving any Binding errors. Would anyone know what the issue might be?
are you sure you are using INotifyPropertyChangedin your View Model ?
it looks like you are setting your other properties in the constructor and then setting your Bitmap property later on. in that case you must Raise the PropertyChanged Event .
I'm stuck again at some data-binding issue.
This time I want to bind a ListView to the SelectedItem of a GridView. I already suceed with this type of data-binding but now my ListView, which should show some details about my selected item in my GridView just stays empty. There are no items in it although they should exist.
The GridView binds just fine at the property in my MainViewModel.Substituting the ElementName attribute with x:Resouces doesn't seem to be an option, because it doesn't work either.
The source view:
<GridView x:Name="gridViewOrderYears"
ItemsSource="{Binding SelectedCustOrders, Mode=TwoWay}"
HorizontalAlignment="Left"
Grid.Row="1"
VerticalAlignment="Top"
Width="316"
Height="63"
Margin="657,316,0,0"
SelectionMode="Single">
<GridView.ItemTemplate>
<DataTemplate>
<Grid>
<Border BorderThickness="1" BorderBrush="Aquamarine">
<StackPanel>
<TextBlock Text="{Binding Year}" FontSize="20"/>
<TextBlock Text="{Binding OrderCount}"/>
</StackPanel>
</Border>
</Grid>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
this View doesnt bind:
<ListView HorizontalAlignment="Left"
Height="231"
Margin="657,401,0,0"
Grid.Row="1"
VerticalAlignment="Top"
Width="316"
DataContext="{Binding SelectedItem, ElementName=gridViewOrderYears}">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding DoneOrders.Order_Date, ElementName=gridViewOrderYears}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
SelectedCustOrders porperty is an IList<OrderYears>.
OrderYears is following data value object defined in my MainViewModel:
public class OrderYears
{
public int? Year { get; set; }
public IList<Orders> DoneOrders { get; set; }
public int OrderCount { get; set; }
}
I think the problem is in the ListView binding, because you try to bind to a property named "Orders", which does not exist in the OrderYears object. You have a property named DoneOrders which you can bind to (don't confuse the property name with the type of elements inside the list!), but if you bind a TextBlock to a IList you will just get the guid for the IList object.
Try something like this, replacing you ListView with a ListBox (which is enough for what you are trying to do here):
<ListBox DataContext="{Binding ElementName=gridViewOrderYears,
Path=SelectedItem.DoneOrders}"
DisplayMemberPath="Order_Date"/>
There is no need to create a template, the items inside the ListBox will be displayed like a TextBlock. Note that you can benefit from binding to nested properties like MainProperty.SubProperty.
Let me know if this was helpful, bindings can be such a headache when you are starting...
Finally I got it.
After hours of trial-and-error finally substituting DataContext with ItemsSource did the trick... Sometimes things are easier than you think :)