ListBox in RowDetail binding failure - c#

I have a WPF application. It contains OrderBlock object which contains other objects, plesase see a brief view of the class.
public class OrderBlocks
{
private List<Order> _orders;
[XmlElement("tF_Transactions")]
public List<Order> Orders { get { return _orders; } set { _orders = value; OnPropertyChanged("Orders"); } }
}
public class Order : INotifyPropertyChanged
{
[XmlIgnore]
public List<Duplications> DuplicateHolder { get; set; }
}
public class Duplications
{
public string ID { get; set; }
public string Name { get; set; }
public Duplications(string newID, string newName)
{
ID = newID;
Name = newName;
}
}
I have a datagrid that is bound to my object Orders of type List Orders. My datagrid has a row detail so that when a user clicks on a row further details are displayed. I have added a listbox to this row detail. I want this row detail to show a listbox which displays my object DuplicateHolder of type List Duplications.
At the moment the listbox is empty. Please see my attempted XAML code below. Any help would be great as always.
<ListBox Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="3" Name="lbIdentifier" SelectionMode="Single" DataContext="{Binding OrderBlock}" HorizontalContentAlignment="Stretch">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Margin="0,2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="{Binding Path=DuplicateHolder.ID}" FontSize="10" HorizontalAlignment="Left" Margin="5,0,0,0"/>
<TextBlock Grid.Column="1" Text="{Binding Path=DuplicateHolder.Name}" FontSize="10" HorizontalAlignment="Left" Margin="5,0,0,0"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

Try this
<Listbox ItemSource = {Binding DuplicateHolder}/>
and
<TextBlock Grid.Column="0" Text="{Binding Path=ID}".../>

t seems like you did not set the bindings correctly because the listbox Context should be a list of Duplications and the ItemTemplate should be for one Duplication instance from the list of duplicates. So if the global datacontext is an instance of OrderBlocks the listbox will be bound to the DuplicateHolder of an Order:
<ListBox Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="3" Name="lbIdentifier" SelectionMode="Single" DataContext="{Binding Path=DuplicateHolder}" HorizontalContentAlignment="Stretch">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Margin="0,2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="{Binding Path=ID}" FontSize="10" HorizontalAlignment="Left" Margin="5,0,0,0"/>
<TextBlock Grid.Column="1" Text="{Binding Path=Name}" FontSize="10" HorizontalAlignment="Left" Margin="5,0,0,0"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

Related

Listview not displaying data but showing namespace plus datamodel

I am using the folloowing class but its not allowing me to display show_times in my xaml its just comming up
popcornpk.datamodel.fetchtiming
public class FetchTiming
{
public string id { get; set; }
public string theater_name { get; set; }
public string address { get; set; }
public List<string> show_times { get; set; }
public string screen_id { get; set; }
public string title { get; set; }
}
public class MovieDetail
{
public MovieDetails movie_details { get; set; }
public List<FetchTiming> fetch_timing { get; set; }
}
My Class call
public async Task<MovieDetail> GetMovieShowtimesAsync()
{
string jsonresult = await WCFRESTServiceCall("GET", "movie_details");
var jarray = JsonConvert.DeserializeObject<MovieDetail>(jsonresult);
return jarray;
}
This is my xamlmethod call I hav eno idea what is going on the data is being returned ok but I just cant seem to dispaly it
private async void listViewShowtimes_Loaded(object sender, RoutedEventArgs e)
{
popcornpk_Dal _dal = new popcornpk_Dal();
MovieDetail _showTimes = await _dal.GetMovieShowtimesAsync();
var listView = (ListView)sender;
listView.ItemsSource = _showTimes.fetch_timing.ToList();
}
Xaml Of DataTemplate
<PivotItem x:Name="pvtShowTimes" Header="showtimes">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<ListView x:Name="listViewShowtimes" ItemsSource="{Binding}" Loaded="listViewShowtimes_Loaded">
<DataTemplate>
<StackPanel Height="505">
<TextBlock FontSize="13" x:Name="txtshowtime" Text="{Binding theater_name}" HorizontalAlignment="Left" Margin="19,223,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="212" Foreground="White" Height="29" SelectionChanged="txtTtile_SelectionChanged"/>
<TextBlock FontSize="13" x:Name="txtshow_times" Text="{Binding address}" HorizontalAlignment="Left" Margin="19,223,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="212" Foreground="White" Height="29" SelectionChanged="txtTtile_SelectionChanged"/>
</StackPanel>
</DataTemplate>
</ListView>
</Grid>
</PivotItem>
Below is a screen shot of the app running on the device any help be greatly apreciated.
Ok So i have it at least displaying the thertre name which is good on the show times screen but its still not allowing me to display the show_times field.
<ListView x:Name="listViewShowtimes" ItemsSource="{Binding}" Loaded="listViewShowtimes_Loaded">
<ListView.ItemTemplate>
<DataTemplate>
<Grid Grid.Row="0">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock FontSize="13" Grid.Row="0" Grid.Column="0" x:Name="txtshowtime" Text="{Binding theater_name}" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" Width="212" Foreground="White" Height="29" />
<TextBlock FontSize="13" Grid.Row="1" Grid.Column="0" x:Name="txtshow_times" Text="{Binding show_times}" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" Width="212" Foreground="White" Height="29" />
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
I think what i need to no is how to bind a list of strings through xaml Problem I have i need the show times to appear below the cinema name you see their
You have Text="{Binding show_times}" so In your class FetchTiming,
public List<string> show_times { get; set; }
is a list so if you bind it to a TextBlock, it will simply do ToString(). You have to add inside the template another ListView like:
<TextBlock FontSize="13" Grid.Row="0" Grid.Column="0" x:Name="txtshowtime" Text="{Binding theater_name}" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" Width="212" Foreground="White" Height="29" />
<ListView ItemsSource={Binding show_times} />
And will do the trick.

How to Populate a Dual Column ListBox

I have a ListBox which has two columns. On a button click event I am adding the column data to an ObservableCollection, and I need to bind the collection to the ListBox to display the ObservableCollection data. I am having trouble figuring out how to do this.
MainPage.xaml
<ListBox x:Name="HistoryListBox" ItemsSource="{Binding Items}" Grid.Row="1" Grid.ColumnSpan="2">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="{Binding ConnectionType}"/>
<TextBlock Grid.Column="1" Text="{Binding DateTime}"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
MainPage.xaml.cs
public ObservableCollection<History> Items { get; set; }
public MainPage()
{
InitializeComponent();
Items = new ObservableCollection<History>();
HistoryListBox.DataContext = Items;
}
private void TestButton_Click(object sender, RoutedEventArgs e)
{
...
PopulateHistoryListBox();
}
private void PopulateHistoryListBox()
{
Items.Add(new History { ConnectionType = ConnectionTypeResultTextBlock.Text, DateTime = DateTime.Now });
}
I am not sure how to properly bind the ObservableCollection items to the ListBox? Also, I somehow need to persist this data historically for when the application reloads. How can I do this with IsolatedStorage?
You should assign ItemsSource ,
HistoryListBox.ItemsSource = Items;
If you are using MVVM,
Assign the ViewModel to the page, not to the listBox,
this.DataContext = ViewModel;
<ListBox x:Name="HistoryListBox" ItemsSource="{Binding Items}" Grid.Row="1" Grid.ColumnSpan="2">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="{Binding Network}"/>
<TextBlock Grid.Column="1" Text="{Binding Date}"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<ListBox ItemsSource="{Binding Items, Mode=TwoWay}">
...
</ListBox>

Access TextBlock text in DataTemplate

I am new to C#/XAML coding and I have the following question.
I have this ListView and I have added Aditional Templates to the generated Items
<ListView x:Name="lvItems" HorizontalAlignment="Left" Height="251" Margin="10,42,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.5,0.5" Width="1346" SelectionChanged="lvItems_SelectionChanged" Foreground="{x:Null}" ItemTemplate="{StaticResource Standard500x130ItemTemplate}">
If I go to edit the template I get the following code
<!-- Grid-appropriate 500 by 130 pixel item template as seen in the GroupDetailPage -->
<DataTemplate x:Key="Standard500x130ItemTemplate">
<Grid Height="110"
Width="480"
Margin="10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Border Background="{StaticResource ListViewItemPlaceholderBackgroundThemeBrush}"
Width="110"
Height="110">
<Image Source="{Binding Image}"
Stretch="UniformToFill"
AutomationProperties.Name="{Binding Title}" />
</Border>
<StackPanel Grid.Column="1"
VerticalAlignment="Top"
Margin="10,0,0,0">
<TextBlock Text="{Binding Title}"
Style="{StaticResource TitleTextStyle}"
TextWrapping="NoWrap" />
<TextBlock Text="{Binding Subtitle}"
Style="{StaticResource CaptionTextStyle}"
TextWrapping="NoWrap" />
<TextBlock Text="{Binding Description}"
Style="{StaticResource BodyTextStyle}"
MaxHeight="60" />
</StackPanel>
</Grid>
</DataTemplate>
Now I want to access the Texblocks Title, Subtitle, Description to add data that I have parsed from an XML File. I guess to to that I need to access the Binding of each TextBlock but I have no clue how to do that. Can you help me?
Thanks in advance for your help
You need to set the ItemsSource property of the ListView, and then the fields will populate for each item, based on the template you created.
lvItems.ItemsSource = MyObjectsCollection;
Here I'm assuming MyObjectCollection is a collection of your objects. Judging from your template, the data class should look something like this:
public class TheObject
{
public string Title { get; set; }
public string Subtitle { get; set; }
public string Description { get; set; }
public string Image { get; set; }
}
So MyObjectsCollection should be an array (or IEnumerable, or List) of TheObject objects.

How to know which row was clicked (and access its data) in Grid in WP7?

I have the following XAML:
<Grid x:Name="ContentPanelDaily"
Grid.Row="1"
<Grid.RowDefinitions>
<RowDefinition Height="40" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="60" />
<ColumnDefinition Width="80" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0"
Grid.Column="0"
Style="{StaticResource PhoneTextAccentStyle}"
Margin="0,0,0,0">
First
</TextBlock>
<TextBlock Grid.Row="0"
Grid.Column="1"
Style="{StaticResource PhoneTextAccentStyle}"
Margin="0,0,0,0">
Second
</TextBlock>
</Grid>
How can i detect which row was clicked in Tap event? I have tried to find SelectedRow or something like this but it seems there is no anything like this in Grid. Thank you very much.
Put your data into a button, and subscribe to on OnClick event.
<Button OnClick="evetnHandler">
<TextBlock Grid.Row="0"
Grid.Column="0"
Style="{StaticResource PhoneTextAccentStyle}"
Margin="0,0,0,0">
First
</TextBlock>
</Button>
Also, as #Rachel said: you can use a ListBox.
<ListBox ItemsSource="{Binding Items}"
SelectedItem="{Binding Selected, Type=TwoWay}">
<ListBox.ItemTemplate>
<DataTemlate>
<TextBlock
Style="{StaticResource PhoneTextAccentStyle}"
Margin="0,0,0,0" Text="{Binding Name}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
And in a code-behind create
public ObservableCollection<DataItem> Items {get;set;}
private DataItem _selected;
public DataItem Selected
{
get {return _selected;}
set
{
_selected = value;
//ha! item selected!!! handle it
}
}
public class DataItem
{
public string Name {get;set;}
}

Let TextBox fill remaining Space in Grid

I have a ListBox that contains a Grid.
The Grid has a Label Column, a GridSplitter and a TextBox Column.
How can I make the Grid take up the entire Width of the ListBox and make the TextBox use the entire space of the Grid Column?
<Grid>
<ListBox x:Name="myList" Margin="4" ItemsSource="{Binding Path=Parameter}" MinHeight="60" Grid.IsSharedSizeScope="True" HorizontalAlignment="Stretch" >
<ListBox.ItemTemplate>
<DataTemplate>
<Grid x:Name="parameterGrid" HorizontalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition x:Name="labelColumn" Width="Auto" SharedSizeGroup="LabelColumn"/>
<ColumnDefinition x:Name="splitterColumn" Width="5"/>
<ColumnDefinition x:Name="textColumn" Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Path=Key}" Grid.Column="0"/>
<GridSplitter Grid.Column="1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"/>
<TextBox Grid.Column="2" Text="{Binding Path=Value, UpdateSourceTrigger=LostFocus}"
IsEnabled="{Binding ElementName=_view, Path=DataContext.IsEditEnabled}">
</TextBox>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
The Parameter is just to demonstrate the idea:
public class Parameter
{
public string Key { get; set; }
public string Value { get; set; }
public Parameter(string key, string value)
{
Key = key;
Value = value;
}
}
Try setting HorizontalContentAlignment="Stretch" on the ListBox.
Dont give any Width and Height for TextBox.

Categories

Resources