WP7 List Box alignment and styling issues - silverlight

I want to create a windows phone screen which will display the some users info in a listbox like following
Right now i am doing this using following code its working fine.
<ListBox>
<ListBoxItem Height="100">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="60"/>
<RowDefinition Height="40"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Image Source="Assets/Users/dummyUser2.png" Grid.RowSpan="2" Grid.Column="0" Height="90" Width="90" />
<TextBlock Text="John Smith" Style="{StaticResource ProfileNameStyleForTextBlock}" />
<TextBlock Text="Birthday" Style="{StaticResource EventNameStyleForTextBlock}" />
<TextBlock Text="Today" Style="{StaticResource EventDateStyleForTextBlock}" Foreground="#09aba9" />
</Grid>
</ListBoxItem>
...........
......
</ListBox>
What i want to do is, i want to load the items into list box dynamically from code behind and the view should be look like above image.
Is it possible to create a style/control like this and add it to all the lists i'll create in future.
Edit
My ListBox
<ListBox Name="lstlist" ItemTemplate="{StaticResource GuyDataTemplate}" />
DataTemplate
<DataTemplate x:Key="GuyDataTemplate">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="60"/>
<RowDefinition Height="40"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Image Source="Assets/Users/dummyUser2.png" Grid.RowSpan="2" Grid.Column="0" Height="90" Width="90" />
<TextBlock Text="{Binding Name}" Style="{StaticResource ProfileNameStyleForTextBlock}" />
<TextBlock Text="{Binding Event}" Style="{StaticResource EventNameStyleForTextBlock}" />
<TextBlock Text="{Binding Date}" Style="{StaticResource EventDateStyleForTextBlock}" Foreground="#09aba9" />
</Grid>
</DataTemplate>
I created a class as
public class Guy
{
public string Name { get; set; }
public string Event { get; set; }
public string Date { get; set; }
}
and wrote the logic in a buttonclick
ObservableCollection<Guy> Guys = new ObservableCollection<Guy>();
Guys.Add(new Guy() { Name = "xyzabc", Event = "event details", Date = "19/25/0258" });
Guys.Add(new Guy() { Name = "xyzabc", Event = "event details", Date = "19/25/0258" });
Guys.Add(new Guy() { Name = "xyzabc", Event = "event details", Date = "19/25/0258" });
Guys.Add(new Guy() { Name = "xyzabc", Event = "event details", Date = "19/25/0258" });
lstlist.ItemsSource = Guys;
After adding this app got stucked

define DataTemplate for ListBox item in resources :
<DataTemplate x:Key="GuyDataTemplate">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="60"/>
<RowDefinition Height="40"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Image Source="Assets/Users/dummyUser2.png" Grid.RowSpan="2" Grid.Column="0" Height="90" Width="90" />
<TextBlock Text="John Smith" Style="{StaticResource ProfileNameStyleForTextBlock}" FontWeight="ExtraBold" />
<TextBlock Text="Birthday" Style="{StaticResource EventNameStyleForTextBlock}" />
<TextBlock Text="Today" Style="{StaticResource EventDateStyleForTextBlock}" Foreground="#09aba9" />
</Grid>
</DataTemplate>
and use it from every ListBox you want:
<ListBox ItemsSource={Binding Guys} ItemTemplate={StaticResource GuyDataTemplate} />
, where Guys is ObservableCollection<>
add new item:
Guys.Add(new Guy() { Name = "Will Snith" });
P.S.
UserControl is not recommended to use as ListBox item

Related

WPF - Windows Media Import Importable Item List View Data Binding

I am using the Windows Media Import API in a WPF Application using Microsoft Docs import-media-from-a-device as an example. I have nearly got it working now but have yet to get the data binding working to provide the list of importable items so any help with this would be much appreciated. The source code can be downloaded here.
Main Window XAML
enter<Window x:Class="ImportProofOfConcept.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ImportProofOfConcept"
mc:Ignorable="d"
Title="Media Import" Height="450" Width="800" Loaded="Window_Loaded">
<Grid Margin="6">
<Grid.RowDefinitions>
<RowDefinition Height="100" />
<RowDefinition Height="*"/>
<RowDefinition Height="50"/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<StackPanel Grid.ColumnSpan="3">
<Label Content="Select the required source to import photos and videos from and then select the required files and click OK."/>
<Label Content="Source:"/>
<ComboBox x:Name="CboSources" SelectionChanged="CboSources_SelectionChanged"/>
<Label Content="Files:"/>
</StackPanel>
<ListView Grid.Row="1" Grid.ColumnSpan="3" MinHeight="10" VerticalAlignment="Stretch" x:Name="fileListView"
HorizontalAlignment="Left" Margin="10,11,0,21"
Width="756"
BorderBrush="#FF858585"
BorderThickness="1"
ScrollViewer.VerticalScrollBarVisibility="Visible">
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.05*"/>
<ColumnDefinition Width="0.20*"/>
<ColumnDefinition Width="0.75*"/>
</Grid.ColumnDefinitions>
<CheckBox Grid.Column="0" IsChecked="{Binding IsSelected, Mode=TwoWay}" />
<Image Grid.Column="1" Source="{Binding Thumbnail}" Width="120" Height="120" Stretch="Uniform"/>
<TextBlock Grid.Column="2" Text="{Binding Name}" VerticalAlignment="Center" Margin="10,0"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<Button Grid.Row="2" Grid.Column="1" Click="ButtonOK_Click" IsDefault="True" Margin="6">OK</Button>
<Button Grid.Row="2" Grid.Column="2" Click="ButtonCancel_Click" IsCancel="True" Margin="6">Cancel</Button>
<ProgressBar Grid.Row="3" Grid.ColumnSpan="3" x:Name="progressBar" SmallChange="0.01" LargeChange="0.1" Maximum="1"/>
</Grid>
Code Behind
private async void FindItems()
{
this.cts = new CancellationTokenSource();
try
{
this.importSession = this.importSource.CreateImportSession();
// Progress handler for FindItemsAsync
var progress = new Progress<uint>((result) =>
{
System.Diagnostics.Debug.WriteLine(String.Format("Found {0} Files", result.ToString()));
});
this.itemsResult =
await this.importSession.FindItemsAsync(PhotoImportContentTypeFilter.ImagesAndVideos, PhotoImportItemSelectionMode.SelectAll)
.AsTask(this.cts.Token, progress);
// GeneratorIncrementalLoadingClass is used to incrementally load items in the Listview view including thumbnails
this.itemsToImport = new GeneratorIncrementalLoadingClass<ImportableItemWrapper>(this.itemsResult.TotalCount,
(int index) =>
{
return new ImportableItemWrapper(this.itemsResult.FoundItems[index]);
});
// Set the items source for the ListView control
this.fileListView.ItemsSource = this.itemsToImport;
// Log the find results
if (this.itemsResult != null)
{
var findResultProperties = new System.Text.StringBuilder();
findResultProperties.AppendLine(String.Format("Photos\t\t\t : {0} \t\t Selected Photos\t\t: {1}", itemsResult.PhotosCount, itemsResult.SelectedPhotosCount));
findResultProperties.AppendLine(String.Format("Videos\t\t\t : {0} \t\t Selected Videos\t\t: {1}", itemsResult.VideosCount, itemsResult.SelectedVideosCount));
findResultProperties.AppendLine(String.Format("SideCars\t\t : {0} \t\t Selected Sidecars\t: {1}", itemsResult.SidecarsCount, itemsResult.SelectedSidecarsCount));
findResultProperties.AppendLine(String.Format("Siblings\t\t\t : {0} \t\t Selected Sibilings\t: {1} ", itemsResult.SiblingsCount, itemsResult.SelectedSiblingsCount));
findResultProperties.AppendLine(String.Format("Total Items Items\t : {0} \t\t Selected TotalCount \t: {1}", itemsResult.TotalCount, itemsResult.SelectedTotalCount));
System.Diagnostics.Debug.WriteLine(findResultProperties.ToString());
}
if (this.itemsResult.HasSucceeded)
{
// Update UI to indicate success
System.Diagnostics.Debug.WriteLine("FindItemsAsync succeeded.");
}
else
{
// Update UI to indicate that the operation did not complete
System.Diagnostics.Debug.WriteLine("FindItemsAsync did not succeed or was not completed.");
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("Photo import find items operation failed. " + ex.Message);
}
this.cts = null;
}
Remove "ImportableItem" from the binding paths in the XAML markup:
<ListView Grid.Row="1" Grid.ColumnSpan="3" MinHeight="10" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" x:Name="LstItems">
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.05*"/>
<ColumnDefinition Width="0.20*"/>
<ColumnDefinition Width="0.75*"/>
</Grid.ColumnDefinitions>
<CheckBox Grid.Column="0" IsChecked="{Binding IsSelected, Mode=TwoWay}" Click="CheckBox_Click"/>
<TextBlock Grid.Column="2" Text="{Binding Name}" VerticalAlignment="Center" Margin="10,0"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
When it comes to displaying the thumbnail in an Image element, you need to convert the returned IRandomAccessStreamReference:
Bind XAML Image to IRandomAccessStreamReference

Textbox doesn't fit to its content size in an expander

I have a Xaml view where I'm trying to display Textbox inside a grid which is inside an Exander.
<Expander DataContext="{Binding DiagnosticCategories[0].DiagnosticResults[0]}" <!-- For the test -->
Background="Transparent"
Foreground="{StaticResource ActiveForegroundBrush}"
IsExpanded="False">
<Grid Margin="10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" SharedSizeGroup="A" />
<ColumnDefinition Width="*" SharedSizeGroup="A" />
<ColumnDefinition Width="auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<StackPanel
Grid.Row="1"
Grid.Column="0"
VerticalAlignment="Top">
<TextBox
Margin="10"
Background="Transparent"
BorderThickness="0"
FontSize="13"
FontWeight="Light"
Foreground="{StaticResource ActiveForegroundBrush}"
IsReadOnly="True"
Opacity="0.8"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
SelectionBrush="Black"
Text="{Binding FormatedParameters, Mode=OneWay}"
TextWrapping="Wrap" />
</StackPanel>
[...]
</Grid>
</Expander>
However, there is a problem with the Textbox which has a anormal height even if my text is just "aa"...
First, I thought that the problem was with the Grid.Row and the Textbox only fit it so I tried to add a StackPanel which doesn't fit the Grid.Row but it doesn't work. It seems that the problem is in the textbox.
With a TextBlock, I don't have this problem but I need the Textbox to display my text.
You can try RichTextBox.
When I want to use TextBox, I have the same problem under a certain height. I solved the problem by using RichTextBox instead of TextBox.
<Expander DataContext="{Binding DiagnosticCategories[0].DiagnosticResults[0]}" <!-- For the test -->
Background="Transparent"
Foreground="{StaticResource ActiveForegroundBrush}"
IsExpanded="False">
<Grid Margin="10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" SharedSizeGroup="A" />
<ColumnDefinition Width="*" SharedSizeGroup="A" />
<ColumnDefinition Width="auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<StackPanel
Grid.Row="1"
Grid.Column="0"
VerticalAlignment="Top">
<RichTextBox
Margin="10"
Background="Transparent"
BorderThickness="0"
FontSize="13"
FontWeight="Light"
Foreground="{StaticResource ActiveForegroundBrush}"
IsReadOnly="True"
Opacity="0.8"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
SelectionBrush="Black"
Text="{Binding FormatedParameters, Mode=OneWay}"
TextWrapping="Wrap" />
</StackPanel>
[...]
</Grid>
</Expander>
As others mentioned, I'm a little unsure what you are trying to achieve. I added a couple of horizontal and vertical layouts to your example:
<Expander DataContext="{Binding DiagnosticCategories[0].DiagnosticResults[0]}"
Background="Yellow" HorizontalAlignment="Center" VerticalAlignment="Center" MinWidth="200"
IsExpanded="False">
<Grid Margin="10" Background="Blue">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" SharedSizeGroup="A" />
<ColumnDefinition Width="*" SharedSizeGroup="A" />
<ColumnDefinition Width="auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<StackPanel
Grid.Row="1"
Grid.Column="0"
VerticalAlignment="Top">
<TextBox
Margin="10"
Background="Orange"
BorderThickness="0"
FontSize="13"
FontWeight="Light"
IsReadOnly="True"
Opacity="0.8"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
SelectionBrush="Black"
Text="Test Text"
TextWrapping="Wrap"
/>
</StackPanel>
</Grid>
</Expander>
And I get the following:
Closed:
Open:

WPF MVVM ListBox does not display last record

I find the problem I have very strange.
I have a WPF application using MVVM pattern.
I retrieve data from my database using Linq-to-SQL and display the Description field data in a ListBox using binding.
It works, but not completely. For some reason the last record's does not display. I have verified it is fetched from the database. It cannot be selected; its not there at all.
The only way to get the record to appear is by setting focus to the ListBox and either scrolling the mouse scroller or actually moving down using the keyboard to past the last record (which is supposed to be the second last record). Once you do that, the record appears.
Another way to get the record to show up is update any of the records in the ListBox. I have a TextBox bound in two-way mode to the SelectedItem of the ListBox, So when I select an item, change the text in the TextBox and click the Update button, it calls the Command to do the database update, which works fine, but then also the missing record of the last record shows up.
Is there a rendering problem with ListBox and ListView, because I have tried both? Why does it seem like the ListBox needs to be "redrawn" before the last items appears?
CategoryModel.cs
public class CategoryModel
{
public int CategoryID { get; set; }
public string Description { get; set; }
public List<CategoryModel> categories = new List<CategoryModel>();
readonly SalesLinkerDataContext _dbContext = new SalesLinkerDataContext();
public void GetCategories()
{
categories.Clear();
var result = _dbContext.tblSalesCategories.ToList();
foreach (var item in result)
{
categories.Add(new CategoryModel
{
CategoryID = item.CategoryID,
Description = item.Description.Trim()
});
}
}
internal void Update(CategoryModel cm)
{
try
{
var category = (from a in _dbContext.tblSalesCategories
where a.CategoryID == cm.CategoryID
select a).FirstOrDefault();
if (category != null)
{
category.Description = cm.Description;
_dbContext.SubmitChanges();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
CategoriesViewModel.cs
public class CategoriesViewModel : ViewModelBase, IPageViewModel
{
public CategoryModel Categories = new CategoryModel();
private DelegateCommand _getCategoriesCommand;
private DelegateCommand _updateCategoryCommand;
/// <summary>
/// Describes the name that will be used for the menu option
/// </summary>
public string Name
{
get { return "Manage Categories"; }
}
public string Description
{
get { return Categories.Description; }
set
{
Categories.Description = value;
OnPropertyChanged("Description");
}
}
public List<CategoryModel> ReceivedCategories
{
get { return Categories.categories; }
set
{
Categories.categories = value;
OnPropertyChanged("ReceivedCategories");
}
}
public ICommand GetCategoriesCommand
{
get
{
if (_getCategoriesCommand == null)
{
_getCategoriesCommand = new DelegateCommand(GetCategories, CanGetCategories);
}
return _getCategoriesCommand;
}
}
private bool CanGetCategories()
{
return true;
}
private void GetCategories()
{
Categories.GetCategories();
ReceivedCategories = Categories.categories;
}
public ICommand UpdateCategoryCommand
{
get
{
if (_updateCategoryCommand == null)
{
_updateCategoryCommand = new DelegateCommand(UpdateCategory, CanUpdateCategory);
}
return _updateCategoryCommand;
}
}
private bool CanUpdateCategory()
{
return true;
}
private void UpdateCategory()
{
Categories.Update(Categories);
}
}
CategoriesView.xaml
<UserControl x:Class="SalesLinker.CategoriesView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="600" Background="White" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<i:InvokeCommandAction Command="{Binding GetCategoriesCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
<Grid >
<Grid.RowDefinitions>
<RowDefinition Height="45"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="250"/>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0" Margin="20,0,0,0" FontSize="20" HorizontalAlignment="Center" Content="Categories"/>
<ListView x:Name="LstCategories" ItemsSource="{Binding ReceivedCategories, Mode=TwoWay}" Grid.Column="0" Grid.Row="1"
VerticalAlignment="Stretch"
Background="LightGray"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
SelectionChanged="LstCategories_OnSelectionChanged">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" VerticalAlignment="Stretch" >
<TextBlock Text="{Binding Description }"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<Button Command="{Binding AddCategoryCommand}" Grid.Column="1" Grid.Row="1" VerticalAlignment="Top" Height="50" Width="50" Margin="0,20,0,0" Background="Transparent" BorderThickness="0" BorderBrush="Transparent" >
<Image Source="/Images/Plus.png"/>
</Button>
<Button Command="{Binding RemoveCategoryCommand}" Grid.Column="1" Grid.Row="1" VerticalAlignment="Top" Height="50" Width="50" Margin="0,75,0,0" Background="Transparent" BorderThickness="0">
<Image Source="/Images/Minus.png"/>
</Button>
<Grid Grid.Row="1" Grid.Column="2">
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="50"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="75"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Label VerticalAlignment="Center" HorizontalAlignment="Center" Grid.Row="0" Grid.Column="0" Content="Description:"/>
<TextBox DataContext="CategoryModel" Grid.Row="0" Grid.Column="1" Width="250" Height="Auto" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="10,0,0,0"
Text="{Binding SelectedItem.Description, ElementName=LstCategories}"/>
<Button Command="{Binding UpdateCategoryCommand}"
Grid.Row="1" Grid.Column="1" HorizontalAlignment="Left" Margin="10,0,0,0" Height="20" Width="120" Content="Update Description"/>
</Grid>
</Grid>
I am very new to MVVM so hopefully I am just not seeing something simple.
For some reason the last record's does not display. I have verified it is fetched from the database.
If your fetch all data from database, then it can be concluded that why you cannot see your last item is as you use using not correct layout. I mean static layout.
I suggest you to use dynamic layout, not static. I mean it is bad:
<Grid.RowDefinitions>
<RowDefinition Height="45"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="250"/>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
The following code is better and it affords you to see all items to be seen and to be resized accoriding the display and Width and Height of your Window or UserControl:
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2*"/>
<ColumnDefinition Width="2*"/>
<ColumnDefinition Width="2*"/>
</Grid.ColumnDefinitions>
Let me show the full example:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2*"/>
<ColumnDefinition Width="2*"/>
<ColumnDefinition Width="2*"/>
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0" Margin="20,0,0,0" FontSize="20" HorizontalAlignment="Center" Content="Categories"/>
<ListView x:Name="LstCategories" ItemsSource="{Binding ReceivedCategories, Mode=TwoWay}" Grid.Column="0" Grid.Row="1"
VerticalAlignment="Stretch"
Background="LightGray"
ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" VerticalAlignment="Stretch" >
<TextBlock Text="{Binding Description }"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<Button Command="{Binding AddCategoryCommand}" Grid.Column="1" Grid.Row="1" VerticalAlignment="Top" Height="50" Width="50" Margin="0,20,0,0" Background="Transparent" BorderThickness="0" BorderBrush="Transparent" >
<Image Source="/Images/Plus.png"/>
</Button>
<Button Command="{Binding RemoveCategoryCommand}" Grid.Column="1" Grid.Row="1" VerticalAlignment="Top" Height="50" Width="50" Margin="0,75,0,0" Background="Transparent" BorderThickness="0">
<Image Source="/Images/Minus.png"/>
</Button>
<Grid Grid.Row="1" Grid.Column="2">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1.5*"/>
<ColumnDefinition Width="2*"/>
</Grid.ColumnDefinitions>
<Label VerticalAlignment="Center" HorizontalAlignment="Center" Grid.Row="0" Grid.Column="0" Content="Description:"/>
<TextBox DataContext="CategoryModel" Grid.Row="0" Grid.Column="1" Height="Auto" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="10,0,0,0"
Text="{Binding SelectedItem.Description, ElementName=LstCategories}"/>
<Button Command="{Binding UpdateCategoryCommand}"
Grid.Row="1" Grid.Column="1" HorizontalAlignment="Left" Content="Update Description"/>
</Grid>
Update:
In order to dispel any doubts, I've made a test to show that all items are shown:
I've made some test model class:
public class FooClass
{
public string Description { get; set; }
}
And I've populated your ListView in a loop:
public MainWindow()
{
InitializeComponent();
PopulateCollection();
}
private void PopulateCollection()
{
List<FooClass> fooColl = new List<FooClass>();
for (int i = 0; i <= 1000; i++)
{
fooColl.Add(new FooClass() { Description=i.ToString()});
}
LstCategories.ItemsSource = fooColl;
}
XAML:
<ListView x:Name="LstCategories" Grid.Column="0" Grid.Row="1"
VerticalAlignment="Stretch"
Background="LightGray"
ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" VerticalAlignment="Stretch" >
<TextBlock Text="{Binding Description }"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
The result is:

How to use only 1 grid layout for both header and detail with itemscontrol?

Now, when i want to create a table with a header and detail i have to define a columns 2 times for each row. the first row is header and the second row i used itemscontrol to binding data.
below is my sample code.
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="{StaticResource WidthColumn}" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="40" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="{Binding ListTitle}"/>
<TextBlock Grid.Column="1" Text="{Binding ListDetailTitle}" />
<TextBlock Grid.Column="2" Text="{Binding ListValueTitle}" />
</Grid>
<StackPanel Grid.Row="1">
<ItemsControl ItemsSource ="{Binding Samples}">
<ItemsControl.Resources>
<DataTemplate DataType="{x:Type parameters:SampleParameters}">
<Grid >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="{StaticResource WidthColumn}" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="40" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="{Binding Position}"/>
<TextBox Grid.Column="1" DataContext="{Binding Name}" Text="{Binding Value}"/>
<Button Grid.Column="2" Name="Button"/>
</Grid>
</DataTemplate>
</ItemsControl.Resources>
</ItemsControl>
</StackPanel>
</Grid>
Can it use only 1 time grid layout? or there is another solution to do with?

Create a Master/Details View based on TreeView Control in Silverlight

I have a TreeNode class with following properties
public string Name { get; set; }
public string Description { get; set; }
public bool AllowMultiples { get; set; }
private ObservableCollection<TreeNode> _childNodes = new ObservableCollection<TreeNode>();
In my UI I want to display tree in left panel and selected items details in right side.
My XAML looks like following:
<UserControl.Resources>
<win:HierarchicalDataTemplate x:Key="ChildTemplate" ItemsSource="{Binding ChildNodes}">
<TextBlock FontStyle="Italic" Text="{Binding Path=Name}" />
</win:HierarchicalDataTemplate>
<win:HierarchicalDataTemplate x:Key="NameTemplate"
ItemsSource="{Binding Path=ChildNodes}"
ItemTemplate="{StaticResource ChildTemplate}">
<TextBlock Text="{Binding Path=Name}" FontWeight="Bold" />
</win:HierarchicalDataTemplate>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="20"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock Text="Document Hierarchy" Margin="0,0,43,0" FontSize="13" />
<toolkit:TreeViewDragDropTarget Grid.Row="1" Grid.Column="0" BorderThickness="-1">
<controls:TreeView BorderThickness="0" ItemTemplate="{StaticResource ChildTemplate}" x:Name="treeView" ItemsSource="{Binding}">
</controls:TreeView>
</toolkit:TreeViewDragDropTarget>
<TextBlock Text="Properties" FontSize="11" Grid.Column="1" Grid.Row="0" />
<Grid Grid.Row="1" Grid.Column="1">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" ></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock x:Name="AlloMultPropLabel" Text="Allow Multiple" Grid.Column="0" Grid.Row="0"></TextBlock>
<RadioButton x:Name="AllowMulti" GroupName="GrpAllowMulti" Content="True" Grid.Column="1" Grid.Row="0"/>
<RadioButton x:Name="AllowMulti2" GroupName="GrpAllowMulti" Content="False" Grid.Column="2" Grid.Row="0" IsChecked="True" />
<TextBlock x:Name="DescPropLabel" Text="Description" HorizontalAlignment="Left" Width="74" Grid.Row="2" ></TextBlock>
<TextBox x:Name="DescProp" Grid.Column="1" Grid.Row="2" Grid.ColumnSpan="2" Text="{Binding Description}" />
</Grid>
Using http://www.silverlight.net/learn/quickstarts/bindingtocontrols/ as my reference I have set the LayoutRoot.DataContext to CollectionViewSource as below:
public ObservableCollection<TreeNode> itemsSource = new ObservableCollection<TreeNode>()
{
new TreeNode("Root", "ths is test"),
new TreeNode("Secondary","Second node"),
};
public MainPage()
{
InitializeComponent();
//treeView.DataContext = itemsSource;
LayoutRoot.DataContext = new CollectionViewSource { Source = itemsSource };
}
When I run this project I notice that the description is always set to first items description. It is not getting changed based on selection.
Any pointers to get this right?
You have bound the TextBox to "{Binding Description}". It will using the same DataContext that the TreeView starts with hence it shows the description of the top item.
In order to for the textbox to follow the selected item in the tree its data context needs to be bound to the SelectedItem of the Treeview. I'd be inclined to placing the "Detail" display in its own Grid and manage its layout in there, this would be a good place to update the DataContext:-
<Grid DataContext="{Binding SelectedItem, ElementName=treeView}">
<Grid.ColumnDefinitions>
<Grid.ColumnDefinition Width="Auto" />
<Grid.ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<Grid.RowDefinition Height="Auto" />
<Grid.RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock Text="Name" Grid.Row="0" Grid.Column="0" />
<TextBox Text="{Binding Name}" Grid.Row="0" Grid.Column="1"/>
<TextBlock Text="Description" Grid.Row="1" Grid.Column="0" />
<TextBox Text="{Binding Description}" Grid.Row="1" Grid.Column="1"/>
</Grid>
Note the binding on the Grid DataContext it uses the treeView as the source object and binds to the SelectedItem.

Resources