Ihave this scenario: A ListView and every Cell is a frame. When showing the list all frames backgroudcolor is white when i click on frame i want the color to change in blie. I change the color but not refreshing. This is my code:
In xaml page
<pages:PopupPage.Resources>
<local1:ChangeFrameBackgroudColor x:Key="ChangeFrameBackgroudColor" />
</ResourceDictionary>-->
</pages:PopupPage.Resources>
<ListView x:Name="IzberiFirmaListView" HasUnevenRows="True" ItemsSource="{Binding KorisnikFirmi}" SelectedItem="{Binding IzbranaFirmaId } " Header="{Binding}" ItemTapped="IzberiFirmaListView_ItemTapped">
<ListView.ItemTemplate >
<DataTemplate>
<local:ExtendedViewCell SelectedBackgroundColor="#2188ff" >
<StackLayout Padding="20, 10" >
<Frame x:Name="frameLabel" BorderColor="#2188ff" BackgroundColor="{Binding IsActive, Converter={StaticResource ChangeFrameBackgroudColor}}">
<Label FontAttributes="Bold" FontSize="18" TextColor="Black" Text="{Binding Naziv}" ></Label>
</Frame>
</StackLayout>
</local:ExtendedViewCell>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.HeaderTemplate>
<DataTemplate>
<ContentView BackgroundColor="#006BE6" >
<Label Margin="10" HorizontalOptions="CenterAndExpand" Text="ОДБЕРЕТЕ ФИРМА" TextColor="White" FontSize="20" FontAttributes="Bold"/>
</ContentView>
</DataTemplate>
</ListView.HeaderTemplate>
</ListView>
In xaml.cs Page:
private void IzberiFirmaListView_ItemTapped(object sender, ItemTappedEventArgs e)
{
var vm = BindingContext as OdberiFirmaPopupViewModel;
var firm = e.Item as SysDashFirma;
vm.ChangeColorOnItemSelected(firm);
}
In viewModel
public List<SysDashFirma> KorisnikFirmi
{
get { return korisnikFirmi; }
set
{
if (korisnikFirmi != value)
{
korisnikFirmi = value;
SetProperty(ref korisnikFirmi, value);
OnPropertyChanged("KorisnikFirmi");
}
}
}
public void ChangeColorOnItemSelected(SysDashFirma firm)
{
if (_oldFirmSelected == firm)
{
//firm.BackColor = "#2188ff";
firm.IsActive = true;
UpdateSelectedFirmItemColor(firm);
}
else
{
if(_oldFirmSelected != null)
{
//_oldFirmSelected.BackColor = "#f5f5f5";
_oldFirmSelected.IsActive = false;
UpdateSelectedFirmItemColor(_oldFirmSelected);
}
// firm.BackColor = "#2188ff";
firm.IsActive = true;
UpdateSelectedFirmItemColor(firm);
}
_oldFirmSelected = firm;
}
private void UpdateSelectedFirmItemColor(SysDashFirma firm)
{
var index = KorisnikFirmi.IndexOf(firm);
KorisnikFirmi.Remove(firm);
KorisnikFirmi.Insert(index,firm);
}
Changes are made in the list ,flag is changed, probably cant refresh the bidning context to converter read all from start.
Use OnPropertyChanged for the IsActive property to notify the UI about its change.
public class Firm : INotifyPropertyChanged
{
private string name = "Unknown";
public string Name
{
get
{
return name;
}
set
{
name = value;
OnPropertyChanged();
}
}
private bool isActive;
public bool IsActive
{
get
{
return isActive;
}
set
{
isActive = value;
OnPropertyChanged();
}
}
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
}
I would suggest changing the IsActive property alone as removing and adding the item is not necessary.
<ListView
ItemTapped="ListView_ItemTapped"
ItemsSource="{Binding FirmCollection}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Frame
Padding="2"
BackgroundColor="{Binding IsActive, Converter={StaticResource converterTest}}">
<Label Text="{Binding Name}"/>
</Frame>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
ViewModel
public class ViewModel : INotifyPropertyChanged
{
....
....
private Firm oldSelectedFirm;
public Firm OldSelectedFirm
{
get
{
return oldSelectedFirm;
}
set
{
oldSelectedFirm = value;
OnPropertyChanged();
}
}
...
...
}
Xaml.cs
void ListView_ItemTapped(System.Object sender, Xamarin.Forms.ItemTappedEventArgs e)
{
var tappedFirm = (e.Item as Firm);
var vm = (BindingContext as ViewModel);
if (vm.OldSelectedFirm != null)
vm.OldSelectedFirm.IsActive = false;
tappedFirm.IsActive = true;
vm.OldSelectedFirm = tappedFirm;
}
Related
I want to create a ListView which contains the department name. The ListView contains the CheckBox with department names. An user can check and unchecked the department and also the user can on clicking on select all check box user can select all department.
Which listview you want either a simple listview with textcell or imagecellits upto you, Here I'm posting code for listview with imagecell, also cell swipe option and just add Checkbox where you want to with its event and apply logics. Hope it works for you!
<AbsoluteLayout>
<ListView x:Name="Demolist" BackgroundColor="White" ItemSelected="Demolist_ItemSelected">
<ListView.ItemTemplate>
<DataTemplate>
<ImageCell Height="30"
Text="{Binding deparment_name }"
Detail="{Binding department_description}"
ImageSource="ImageName.png">
<ImageCell.ContextActions>
<MenuItem x:Name="OnMore" Clicked="OnMore_Clicked" CommandParameter="{Binding .}" Text="More" />
<MenuItem x:Name="OnDelete" Clicked="OnDelete_Clicked" CommandParameter="{Binding .}" Text="Delete" IsDestructive="True" />
</ImageCell.ContextActions>
</ImageCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</AbsoluteLayout>
Checkbox is not a control present in XF framework, so I think you can not add checkbox in listview in Xamarin.form, but you can use different to display check and uncheck status.
<ContentPage
x:Class="test2.Page3"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:convert="clr-namespace:test2"
x:Name="ToDoPage">
<ContentPage.Resources>
<convert:converter1 x:Key="converterbool" />
</ContentPage.Resources>
<ContentPage.Content>
<StackLayout>
<ListView x:Name="listview1" ItemsSource="{Binding todoList}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="4*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Label Text="{Binding ItemDescription}" VerticalOptions="Center" />
<Button
Grid.Column="1"
Command="{Binding Source={x:Reference ToDoPage}, Path=BindingContext.UpdateCheckBoxCommand}"
CommandParameter="{Binding Id}"
Opacity="0" />
<Image
Grid.Column="1"
HeightRequest="20"
HorizontalOptions="Center"
Source="{Binding IsDone, Converter={StaticResource converterbool}}"
VerticalOptions="Center" />
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage.Content>
public class TodoItem:INotifyPropertyChanged
{
private string _Id;
public string Id
{
get { return _Id; }
set
{
_Id = value;
RaisePropertyChanged("Id");
}
}
private string _ItemDescription;
public string ItemDescription
{
get { return _ItemDescription; }
set
{
_ItemDescription = value;
RaisePropertyChanged("ItemDescription");
}
}
private bool _IsDone;
public bool IsDone
{
get { return _IsDone; }
set
{
_IsDone = value;
RaisePropertyChanged("IsDone");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
class ToDoViewModel:INotifyPropertyChanged
{
public ObservableCollection<TodoItem> todoList { get; set; }
public ICommand UpdateCheckBoxCommand { get; set; }
public ToDoViewModel()
{
todoList = new ObservableCollection<TodoItem>()
{
new TodoItem(){ Id = "1", ItemDescription = "Task 1", IsDone = false},
new TodoItem(){ Id = "2", ItemDescription = "Task 2", IsDone = false},
new TodoItem(){ Id = "3", ItemDescription = "Task 3", IsDone = false},
new TodoItem(){ Id = "4", ItemDescription = "Task 4", IsDone = false},
new TodoItem(){ Id = "5", ItemDescription = "Task 5",IsDone=false }
};
UpdateCheckBoxCommand = new Command((Id) => UpdateCheckBox(Id.ToString()));
}
private void UpdateCheckBox(string id)
{
IEnumerable<TodoItem> items = todoList.Where(x=>x.Id==id);
foreach(var item in items )
{
if (item.IsDone) item.IsDone = false;
else item.IsDone = true;
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
class converter1 : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
bool ischeck = (bool)value;
if(ischeck==false)
{
return "uncheck.png";
}
else
{
return "check.png";
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class Page3 : ContentPage
{
public Page3 ()
{
InitializeComponent ();
this.BindingContext = new ToDoViewModel();
}
}
I want a structure like this :
Click this to see the desired output
But with my code i'm getting this : Click this to see the output
Here is my xaml code :
<ScrollView Orientation="Horizontal">
<StackLayout Orientation="Horizontal" VerticalOptions="Start">
<Grid x:Name="ImagesListViews" >
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
</Grid>
<local:BindableStackLayout x:Name="featuredEventsList">
<local:BindableStackLayout.ItemDataTemplate>
<DataTemplate>
<StackLayout Orientation="Vertical" Padding="0" Margin="-5,0,5,0" HorizontalOptions="Center" >
<StackLayout.GestureRecognizers>
<TapGestureRecognizer NumberOfTapsRequired="1" />
</StackLayout.GestureRecognizers>
<Image Source="{Binding ImageThumbURL}" Margin="0,0,0,0" WidthRequest="140" />
<Label Margin="0" Text="{Binding TitleInPrimaryLang}" FontSize="12" TextColor="Black" LineBreakMode="TailTruncation" WidthRequest="100"/>
</StackLayout>
</DataTemplate>
</local:BindableStackLayout.ItemDataTemplate>
</local:BindableStackLayout>
</StackLayout>
</ScrollView>
Any help would be highly appreciated. Thank you
you have to make customize control for this. Please go through this and let me know if any query.
1) Extend Scroll view with Customized template.
public class HorizontalListview : ScrollView
{
public static readonly BindableProperty ItemsSourceProperty =
BindableProperty.Create("ItemsSource", typeof(IEnumerable), typeof(HorizontalListview), default(IEnumerable));
public IEnumerable ItemsSource
{
get { return (IEnumerable)GetValue(ItemsSourceProperty); }
set { SetValue(ItemsSourceProperty, value); }
}
public static readonly BindableProperty ItemTemplateProperty =
BindableProperty.Create("ItemTemplate", typeof(DataTemplate), typeof(HorizontalListview), default(DataTemplate));
public DataTemplate ItemTemplate
{
get { return (DataTemplate)GetValue(ItemTemplateProperty); }
set { SetValue(ItemTemplateProperty, value); }
}
public event EventHandler<ItemTappedEventArgs> ItemSelected;
public static readonly BindableProperty SelectedCommandProperty =
BindableProperty.Create("SelectedCommand", typeof(ICommand), typeof(HorizontalListview), null);
public ICommand SelectedCommand
{
get { return (ICommand)GetValue(SelectedCommandProperty); }
set { SetValue(SelectedCommandProperty, value); }
}
public static readonly BindableProperty SelectedCommandParameterProperty =
BindableProperty.Create("SelectedCommandParameter", typeof(object), typeof(HorizontalListview), null);
public object SelectedCommandParameter
{
get { return GetValue(SelectedCommandParameterProperty); }
set { SetValue(SelectedCommandParameterProperty, value); }
}
public void Render()
{
if (ItemTemplate == null || ItemsSource == null)
return;
var layout = new StackLayout();
layout.Spacing = 0;
layout.Orientation = Orientation == ScrollOrientation.Vertical ? StackOrientation.Vertical : StackOrientation.Horizontal;
foreach (var item in ItemsSource)
{
var command = SelectedCommand ?? new Command((obj) =>
{
var args = new ItemTappedEventArgs(ItemsSource, item);
ItemSelected?.Invoke(this, args);
});
var commandParameter = SelectedCommandParameter ?? item;
var viewCell = ItemTemplate.CreateContent() as ViewCell;
viewCell.View.BindingContext = item;
viewCell.View.GestureRecognizers.Add(new TapGestureRecognizer
{
Command = command,
CommandParameter = commandParameter,
NumberOfTapsRequired = 1
});
layout.Children.Add(viewCell.View);
}
Content = layout;
}
}
2) Add Namespace top to your page.
xmlns:control="clr-namespace:**Projectname**.CustomControls"
3) Use Control,
<control:HorizontalListview Orientation="Horizontal">
<control:HorizontalListview.ItemTemplate>
<DataTemplate>
<ViewCell>
<!....Your Design.....>
</ViewCell>
</DataTemplate>
</control:HorizontalListview.ItemTemplate>
</control:HorizontalListview>
4) Bind your data.
**YourControlName**.ItemsSource = lLstPhotoGallery; // Your List
**YourControlName**.Render();
I have a grouped ObservableCollection which is binded to a ListView.
I have a number of StackLayout inside the ListView who's visibility changes upon the ListView tapp.
All others properties inside reacts to the PropertyChange but the visibility of the StackLayout doesnot.
All this is done on UI Thread.
Can anyone else has/had this issue?
Updating the UI inside a loop
Device.BeginInvokeOnMainThread(() =>
{
item.Effort = SelectedTaskItem.Effort;
finalEffortDouble = SelectedTaskItem.Effort + finalEffortDouble - previousEffort;
item.IsSaved = true;
item.IsNotSaved = false;
item.EffortsString = SelectedTime.ToString("hh':'mm") + " h";
TotalEffortsHoursString = TimeSpan.FromHours(finalEffortDouble + TotalEffortsHours).ToString("hh'.'mm") + " hrs";
});
IsSaved , IsNotSaved are bool which are binded to the StackLayout visibility property inside a ListView.
<StackLayout Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" IsVisible="{Binding IsNotSaved,Mode=TwoWay}" Padding="0" Margin="0" HorizontalOptions="FillAndExpand">
<Label Text="{Binding TaskName}" FontAttributes="Bold" FontSize="16" LineBreakMode="TailTruncation" />
<Label Text="Enter time spent on this task daily" FontSize="12" />
</StackLayout>
<StackLayout Grid.Row="0" Grid.Column="1" IsVisible="{Binding IsNotSaved,Mode=TwoWay}" Orientation="Horizontal" VerticalOptions="CenterAndExpand" Spacing="10" HorizontalOptions="End" Padding="0" Margin="0">
<ffimageloading:CachedImage Source="tsplus">
<ffimageloading:CachedImage.GestureRecognizers>
<TapGestureRecognizer Tapped="EditTime_Tapped" CommandParameter="{Binding .}" />
</ffimageloading:CachedImage.GestureRecognizers>
</ffimageloading:CachedImage>
</StackLayout>
<StackLayout x:Name="Is1" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" IsVisible="{Binding IsSaved,Mode=TwoWay}" Padding="0" Margin="0" HorizontalOptions="FillAndExpand">
<Label Text="{Binding TaskName}" FontAttributes="Bold" FontSize="16" LineBreakMode="TailTruncation" />
<StackLayout Orientation="Horizontal" Padding="0" Margin="0" HorizontalOptions="FillAndExpand">
<Label Text="Tap on time to edit" FontSize="12" />
<ffimageloading:CachedImage Source="savedtag" WidthRequest="60" HeightRequest="20" />
</StackLayout>
</StackLayout>
<StackLayout x:Name="Is2" Grid.Row="0" Grid.Column="1" IsVisible="{Binding IsSaved,Mode=TwoWay}" VerticalOptions="CenterAndExpand" HorizontalOptions="End" Padding="0" Margin="0">
<StackLayout.GestureRecognizers>
<TapGestureRecognizer Tapped="EditTime_Tapped" CommandParameter="{Binding .}" />
</StackLayout.GestureRecognizers>
<Label Text="{Binding EffortsString}" FontSize="24" VerticalOptions="Center" TextColor="{x:Static common:ColorResources.NavigationBarColor}" />
</StackLayout>
Model of the binded ObservableCollection
public class ProjectListWithTasksModel : List<TaskModel>
{
public List<TaskModel> Tasks => this;
public string ProjectName { get; set; }
}
Task Model:
public class TaskModel : ObservableObject
{
public string TaskGUID { get; set; }
public string TaskName { get; set; }
public string EffortStatus { get; set; }
public string TaskStatus { get; set; }
private double _Effort;
public double Effort
{
get { return _Effort; }
set
{
_Effort = value;
RaisePropertyChanged("Effort");
}
}
public DateTime EffortDate { get; set; }
public DateTime EffortDay { get; set; }
//UI Models : Used for UI Changes
public string ProjectID { get; set; }
public DateTime LastWorkingDay { get; set; }
private bool _IsSaved;
public bool IsSaved
{
get
{
if (this.EffortStatus.Contains("10"))
_IsSaved = true;
else
_IsSaved = false;
return _IsSaved;
}
set
{
_IsSaved = value;
RaisePropertyChanged("IsSaved");
}
}
public bool IsSubmitted
{
get
{
if (this.EffortStatus.Contains("20"))
{
return true;
//&& LastWorkingDay.Date >= DateTime.Now.Date
}
else
return false;
}
}
public bool IsApproved
{
get
{
if (this.EffortStatus.Contains("30"))
{
return true;
}
else
return false;
}
}
public bool IsRejected
{
get
{
if (this.EffortStatus.Contains("40"))
{
return true;
}
else
return false;
}
}
public string _EffortsString;
public string EffortsString
{
get
{
_EffortsString = TimeSpan.FromHours(Effort).ToString("hh':'mm") + " h";
return _EffortsString;
}
set
{
_EffortsString = value;
RaisePropertyChanged("EffortsString");
}
}
private bool _IsNotSaved;
public bool IsNotSaved
{
get
{
if (this.EffortStatus.Contains("0 "))
_IsNotSaved = true;
else
_IsNotSaved = false;
return _IsNotSaved;
}
set
{
_IsNotSaved = value;
RaisePropertyChanged("IsNotSaved");
}
}
}
I think the problem is that IsSaved depends by EffortStatus, so you should not have a "Set" for "IsSaved". I think you should "RaiseProperty" IsSaved (and IsNotSaved) when EffortStatus i set. Something like:
public string EffortStatus
{
get
{
return _EffortStatus;
}
set
{
_EffortStatus= value;
if (_EffortStatus.Contains("10"))
IsSaved = true;
else
IsSaved = false;
RaisePropertyChanged("EffortStatus");
RaisePropertyChanged("IsSaved");
}
}
I have a xaml page. xaml page wants to show two TextBlocks and one LonglistSelector.
the two TextBlocks datasource come from an object(SpecifiedArticle);the LonglistSelctor itemsSource comes from Collection(ImageUriCollection).
when the page launch, the images cannot be displayed.
two TextBlocks data show well
LonglistSelctor does not show images; but i am sure ImageUriCollection's data can be get from the xaml because i tested in a image control and it works
<Image Source="{Binding ImageUriCollection[0].ImageSource}" Width="108" Height="108" Stretch="UniformToFill">
</Image>
i think the issue is in the LonglistSelctor itemsSource binding. anyone can help?
all code below(without the httpclient wrapper):
DetailsPage.cs is below:
public partial class DetailsPage : PhoneApplicationPage
{
DetailsPageViewModel viewModel = new DetailsPageViewModel();
public DetailsPage()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(DetailsPage_Loaded);
}
private void DetailsPage_Loaded(object sender, RoutedEventArgs e)
{
DataContext = viewModel;
//imageList.ItemsSource = viewModel.ImageUriCollection;
//imageList.ScrollTo(imageList.ItemsSource[imageList.ItemsSource.Count - 1]);
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
string ArticleId = "";
try
{
if (NavigationContext.QueryString.TryGetValue("ArticleId", out ArticleId))
{
Debug.WriteLine(ArticleId);
viewModel.LoadPage(Int32.Parse(ArticleId));
//Debug.WriteLine(viewModel.SpecifiedArticle.Words.ToString());
//LayoutRoot.DataContext = new CollectionViewSource { Source = viewModel.SpecifiedArticle };
}
else
{
MessageBox.Show("No ArticleId passed in.");
}
}
catch(Exception ex)
{
MessageBox.Show("Error in DetailsPage.OnNavigatedTo");
}
}
}
ViewModel is below:
public class DetailsPageViewModel : INotifyPropertyChanged
{
private bool _isLoading = false;
public bool IsLoading
{
get
{
return _isLoading;
}
set
{
_isLoading = value;
NotifyPropertyChanged("IsLoading");
}
}
public DetailsPageViewModel()
{
this.SpecifiedArticle = new Article();
this.ImageUriCollection = new ObservableCollection<Photo>();
this.IsLoading = false;
}
private Article specifiedArticle;
public Article SpecifiedArticle
{
get
{
return specifiedArticle;
}
set
{
specifiedArticle = value;
NotifyPropertyChanged("SpecifiedArticle");
}
}
public ObservableCollection<Photo> ImageUriCollection
{
get;
private set;
}
public void LoadPage(int articleId)
{
IsLoading = true;
ReadArticle(articleId);
}
private async void ReadArticle(int articleId)
{
try
{
Article articleDetails = new Article();
articleDetails = await CollectionHttpClient.GetAsyncByArticleId(articleId);
SpecifiedArticle = articleDetails;
//articleDetails.FirstImage = new Uri(articleDetails.ImagePathList[0]);
if (articleDetails.ImagePathList != null)
{
foreach (var item in articleDetails.ImagePathList)
{
Photo p = new Photo();
p.ImageSource = new Uri(item);
this.ImageUriCollection.Add(p);
}
//var image = await CollectionHttpClient.GetImageByImageName(articleDetails.ImagePath);
//Bytelist.Add(image);
}
else
{
this.ImageUriCollection = null;
}
IsLoading = false;
}
catch(Exception ex)
{
MessageBox.Show("sorry, no data.");
IsLoading = false;
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (null != handler)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
xaml is:
<phone:PhoneApplicationPage.Resources>
<vm:DetailsPageViewModel x:Key="viewModel"/>
<phone:JumpListItemBackgroundConverter x:Key="BackgroundConverter"/>
<phone:JumpListItemForegroundConverter x:Key="ForegroundConverter"/>
<Style x:Key="JumpListStyle" TargetType="phone:LongListSelector">
<Setter Property="LayoutMode" Value="List" />
<Setter Property="Margin" Value="12,12,0,0"/>
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
<Border Background="{Binding Converter={StaticResource BackgroundConverter}}"
Width="470"
Height="70"
Margin="6">
<TextBlock Text="{Binding Key}"
Foreground="{Binding Converter={StaticResource ForegroundConverter}}"
FontFamily="{StaticResource PhoneFontFamilySemiBold}"
FontSize="28"
Padding="2"
VerticalAlignment="Bottom"/>
</Border>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
<DataTemplate x:Key="GroupHeader">
<Border Background="Transparent">
<Border Background="Transparent" BorderBrush="Transparent" BorderThickness="1"
Width="400" Height="90"
HorizontalAlignment="Left">
<TextBlock Text="Pictures:"
Foreground="{StaticResource PhoneAccentBrush}"
FontSize="28"
Padding="2"
FontFamily="{StaticResource PhoneFontFamilySemiLight}"
HorizontalAlignment="Left"
VerticalAlignment="Center"/>
</Border>
</Border>
</DataTemplate>
<DataTemplate x:Key="ItemTemplate">
<StackPanel Height="108" Width="108" Margin="6,6">
<Image Width="108" Height="108" Stretch="UniformToFill">
<Image.Source>
<BitmapImage UriSource="{Binding ImageSource}" CreateOptions="BackgroundCreation"/>
</Image.Source>
</Image>
</StackPanel>
</DataTemplate>
</phone:PhoneApplicationPage.Resources>
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<!--TitlePanel contains the name of the application and page title-->
<StackPanel Grid.Row="0" Margin="12,17,0,28">
<TextBlock Text="{Binding Path=SpecifiedArticle.Subject }" TextWrapping="Wrap" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<!--ContentPanel - place images here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Background="Transparent">
<!--
<Image Source="{Binding ImageUriCollection[0].ImageSource}" Width="108" Height="108" Stretch="UniformToFill">
<Image.Source>
<BitmapImage UriSource="{Binding ImageSource}" CreateOptions="BackgroundCreation"/>
</Image.Source>
</Image>
-->
<phone:LongListSelector Name="imageList" Margin="13,-30,0,0"
ItemsSource="{Binding ImageUriCollection}"
ItemTemplate="{StaticResource ItemTemplate}"
JumpListStyle="{StaticResource JumpListStyle}"
IsGroupingEnabled="True"
LayoutMode="Grid"
GridCellSize="108,108"/>
</Grid>
<!--ContentPanel - place article words here-->
<StackPanel Grid.Row="2" Margin="12,17,0,28">
<TextBlock Text="{Binding Path=SpecifiedArticle.Words}" TextWrapping="Wrap" Style="{StaticResource PhoneTextNormalStyle}"/>
</StackPanel>
</Grid>
DetailsPage.cs is below:
public partial class DetailsPage : PhoneApplicationPage
{
DetailsPageViewModel viewModel = new DetailsPageViewModel();
public DetailsPage()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(DetailsPage_Loaded);
}
private void DetailsPage_Loaded(object sender, RoutedEventArgs e)
{
DataContext = viewModel;
//imageList.ItemsSource = viewModel.ImageUriCollection;
//imageList.ScrollTo(imageList.ItemsSource[imageList.ItemsSource.Count - 1]);
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
string ArticleId = "";
try
{
if (NavigationContext.QueryString.TryGetValue("ArticleId", out ArticleId))
{
Debug.WriteLine(ArticleId);
viewModel.LoadPage(Int32.Parse(ArticleId));
//Debug.WriteLine(viewModel.SpecifiedArticle.Words.ToString());
//LayoutRoot.DataContext = new CollectionViewSource { Source = viewModel.SpecifiedArticle };
}
else
{
MessageBox.Show("No ArticleId passed in.");
}
}
catch(Exception ex)
{
MessageBox.Show("Error in DetailsPage.OnNavigatedTo");
}
}
}
ViewModel is below:
public class DetailsPageViewModel : INotifyPropertyChanged
{
private bool _isLoading = false;
public bool IsLoading
{
get
{
return _isLoading;
}
set
{
_isLoading = value;
NotifyPropertyChanged("IsLoading");
}
}
public DetailsPageViewModel()
{
this.SpecifiedArticle = new Article();
this.ImageUriCollection = new ObservableCollection<Photo>();
this.IsLoading = false;
}
private Article specifiedArticle;
public Article SpecifiedArticle
{
get
{
return specifiedArticle;
}
set
{
specifiedArticle = value;
NotifyPropertyChanged("SpecifiedArticle");
}
}
public ObservableCollection<Photo> ImageUriCollection
{
get;
private set;
}
public void LoadPage(int articleId)
{
IsLoading = true;
ReadArticle(articleId);
}
private async void ReadArticle(int articleId)
{
try
{
Article articleDetails = new Article();
articleDetails = await CollectionHttpClient.GetAsyncByArticleId(articleId);
SpecifiedArticle = articleDetails;
//articleDetails.FirstImage = new Uri(articleDetails.ImagePathList[0]);
if (articleDetails.ImagePathList != null)
{
foreach (var item in articleDetails.ImagePathList)
{
Photo p = new Photo();
p.ImageSource = new Uri(item);
this.ImageUriCollection.Add(p);
}
//var image = await CollectionHttpClient.GetImageByImageName(articleDetails.ImagePath);
//Bytelist.Add(image);
}
else
{
this.ImageUriCollection = null;
}
IsLoading = false;
}
catch(Exception ex)
{
MessageBox.Show("sorry, no data.");
IsLoading = false;
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (null != handler)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
Try this-
just change your ItemTemplate DataTemplate to a simpler one
If it works, add one change at a time.
<DataTemplate x:Key="ItemTemplate">
<Image Source="{Binding ImageSource}" Stretch="UniformToFill"/>
</DataTemplate>
it is the style problem.
delete the style, try again, images show well
<phone:LongListSelector Name="imageList" Margin="13,-30,0,0"
ItemsSource="{Binding ImageUriCollection}"
ItemTemplate="{StaticResource ItemTemplate}"
LayoutMode="Grid"
GridCellSize="108,108">
</phone:LongListSelector>
in the JumpListStyle, it contains the textblock which is not belong to the xaml, that is why the LonglistSelector does not display anything after binding collectly.
I am creating a multiselect combobox in Windows 8 as shown in below image:
For this I have created custom control code for which is mentioned below:
The problem with below code is that
on selecting all the all items are not selected
Selected items are not displayed in textbox
How can I fix that?
XAML:
<UserControl
x:Class="App5.MultiSelectComboBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App5"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
x:Name="thisUC"
d:DesignWidth="400">
<ComboBox
x:Name="MultiSelectCombo"
ScrollViewer.HorizontalScrollBarVisibility="Auto"
ScrollViewer.VerticalScrollBarVisibility="Auto"
Width="400" Height="20"
>
<ComboBox.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding Title}" Foreground="Black"
IsChecked="{Binding ElementName=thisUC, Path=IsSelected, Mode=TwoWay}"
Click="CheckBox_Click" />
</DataTemplate>
</ComboBox.ItemTemplate>
<ControlTemplate TargetType="ComboBox">
<Grid >
<ToggleButton
x:Name="ToggleButton"
Grid.Column="2" IsChecked="{TemplateBinding IsDropDownOpen}"
ClickMode="Press" HorizontalContentAlignment="Left" >
<ToggleButton.Template>
<ControlTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="18"/>
</Grid.ColumnDefinitions>
<Border
x:Name="Border"
Grid.ColumnSpan="2"
CornerRadius="2"
Background="White"
BorderBrush="Black"
BorderThickness="1,1,1,1" />
<Border
x:Name="BorderComp"
Grid.Column="0"
CornerRadius="2"
Margin="1"
Background="White"
BorderBrush="Black"
BorderThickness="0,0,0,0" >
<TextBlock Foreground="Black" Text="{TemplateBinding TextValue}"
Padding="3" />
</Border>
<Path
x:Name="Arrow"
Grid.Column="1"
Fill="Black"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Data="M 0 0 L 4 4 L 8 0 Z"/>
</Grid>
</ControlTemplate>
</ToggleButton.Template>
</ToggleButton>
<Popup
Name="Popup"
IsOpen="{TemplateBinding IsDropDownOpen}"
>
<Grid
Name="DropDown"
MinWidth="{TemplateBinding ActualWidth}"
MaxHeight="{TemplateBinding MaxDropDownHeight}">
<Border x:Name="DropDownBorder"
BorderThickness="1" Background="White"
BorderBrush="Black"/>
<ScrollViewer Margin="4,6,4,6" DataContext="{Binding}">
<StackPanel />
</ScrollViewer>
</Grid>
</Popup>
</Grid>
<!--<ControlTemplate.Triggers>
<Trigger Property="HasItems" Value="false">
<Setter TargetName="DropDownBorder" Property="MinHeight" Value="95"/>
</Trigger>
<Trigger SourceName="Popup" Property="Popup.AllowsTransparency" Value="true">
<Setter TargetName="DropDownBorder" Property="CornerRadius" Value="4"/>
<Setter TargetName="DropDownBorder" Property="Margin" Value="0,2,0,0"/>
</Trigger>
</ControlTemplate.Triggers>-->
</ControlTemplate>
</ComboBox>
</UserControl>
C#
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236
namespace App5
{
public sealed partial class MultiSelectComboBox : UserControl
{
private ObservableCollection<Node> _nodeList;
public MultiSelectComboBox()
{
InitializeComponent();
_nodeList = new ObservableCollection<Node>();
}
#region Dependency Properties
public static readonly DependencyProperty ItemsSourceProperty =
DependencyProperty.Register("ItemsSource", typeof(Dictionary<string, object>), typeof(MultiSelectComboBox), new PropertyMetadata(null,
new PropertyChangedCallback(MultiSelectComboBox.OnItemsSourceChanged)));
public static readonly DependencyProperty SelectedItemsProperty =
DependencyProperty.Register("SelectedItems", typeof(Dictionary<string, object>), typeof(MultiSelectComboBox), new PropertyMetadata(null,
new PropertyChangedCallback(MultiSelectComboBox.OnSelectedItemsChanged)));
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("TextValue", typeof(string), typeof(MultiSelectComboBox), new PropertyMetadata(null));
public static readonly DependencyProperty DefaultTextProperty =
DependencyProperty.Register("DefaultText", typeof(string), typeof(MultiSelectComboBox), new PropertyMetadata(null));
public Dictionary<string, object> ItemsSource
{
get { return (Dictionary<string, object>)GetValue(ItemsSourceProperty); }
set
{
SetValue(ItemsSourceProperty, value);
}
}
public Dictionary<string, object> SelectedItems
{
get { return (Dictionary<string, object>)GetValue(SelectedItemsProperty); }
set
{
SetValue(SelectedItemsProperty, value);
}
}
public string TextValue
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
public string DefaultText
{
get { return (string)GetValue(DefaultTextProperty); }
set { SetValue(DefaultTextProperty, value); }
}
#endregion
#region Events
private static void OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
MultiSelectComboBox control = (MultiSelectComboBox)d;
control.DisplayInControl();
}
private static void OnSelectedItemsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
MultiSelectComboBox control = (MultiSelectComboBox)d;
control.SelectNodes();
control.SetText();
}
private void CheckBox_Click(object sender, RoutedEventArgs e)
{
CheckBox clickedBox = (CheckBox)sender;
if (clickedBox.Content == "All")
{
if (clickedBox.IsChecked.Value)
{
foreach (Node node in _nodeList)
{
node.IsSelected = true;
}
}
else
{
foreach (Node node in _nodeList)
{
node.IsSelected = false;
}
}
}
else
{
int _selectedCount = 0;
foreach (Node s in _nodeList)
{
if (s.IsSelected && s.Title != "All")
_selectedCount++;
}
if (_selectedCount == _nodeList.Count - 1)
_nodeList.FirstOrDefault(i => i.Title == "All").IsSelected = true;
else
_nodeList.FirstOrDefault(i => i.Title == "All").IsSelected = false;
}
SetSelectedItems();
SetText();
}
#endregion
#region Methods
private void SelectNodes()
{
foreach (KeyValuePair<string, object> keyValue in SelectedItems)
{
Node node = _nodeList.FirstOrDefault(i => i.Title == keyValue.Key);
if (node != null)
node.IsSelected = true;
}
}
private void SetSelectedItems()
{
if (SelectedItems == null)
SelectedItems = new Dictionary<string, object>();
SelectedItems.Clear();
foreach (Node node in _nodeList)
{
if (node.IsSelected && node.Title != "All")
{
if (this.ItemsSource.Count > 0)
SelectedItems.Add(node.Title, this.ItemsSource[node.Title]);
}
}
}
private void DisplayInControl()
{
_nodeList.Clear();
if (this.ItemsSource.Count > 0)
_nodeList.Add(new Node("All"));
foreach (KeyValuePair<string, object> keyValue in this.ItemsSource)
{
Node node = new Node(keyValue.Key);
_nodeList.Add(node);
}
MultiSelectCombo.ItemsSource = _nodeList;
}
private void SetText()
{
if (this.SelectedItems != null)
{
StringBuilder displayText = new StringBuilder();
foreach (Node s in _nodeList)
{
if (s.IsSelected == true && s.Title == "All")
{
displayText = new StringBuilder();
displayText.Append("All");
break;
}
else if (s.IsSelected == true && s.Title != "All")
{
displayText.Append(s.Title);
displayText.Append(',');
}
}
this.TextValue = displayText.ToString().TrimEnd(new char[] { ',' });
}
// set DefaultText if nothing else selected
if (string.IsNullOrEmpty(this.TextValue))
{
this.TextValue = this.DefaultText;
}
}
#endregion
}
public class Node : INotifyPropertyChanged
{
private string _title;
private bool _isSelected;
#region ctor
public Node(string title)
{
Title = title;
}
#endregion
#region Properties
public string Title
{
get
{
return _title;
}
set
{
_title = value;
NotifyPropertyChanged("Title");
}
}
public bool IsSelected
{
get
{
return _isSelected;
}
set
{
_isSelected = value;
NotifyPropertyChanged("IsSelected");
}
}
#endregion
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
I think I understand what you want. Overall, I would recommend a slightly different approach. I would recommend, for one, that you not use a ComboBox to accomplish this - an ItemsControl will accomplish what you want with less work and overhead. Using a Popup like you are is great, but let me show you a slightly simplified way that (this is the best part) works the way you are asking in your question.
Here's your XAML:
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<Grid.DataContext>
<local:ViewModel/>
</Grid.DataContext>
<StackPanel Orientation="Vertical" VerticalAlignment="Center" HorizontalAlignment="Center">
<TextBox Width="200" FontSize="24" Text="{Binding Header, Mode=TwoWay}"
IsReadOnly="True" TextWrapping="Wrap" MaxHeight="200" />
<ScrollViewer VerticalScrollBarVisibility="Auto" MaxHeight="200" Width="200" Background="White">
<ItemsControl ItemsSource="{Binding Items}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding Text}"
FontSize="24"
Foreground="Black"
IsChecked="{Binding IsChecked, Mode=TwoWay}"
IsThreeState="False" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
</StackPanel>
</Grid>
Here's your code-behind:
public class Item : BindableBase
{
public string Text { get; set; }
bool _IsChecked = default(bool);
public bool IsChecked { get { return _IsChecked; } set { SetProperty(ref _IsChecked, value); } }
}
public class ViewModel : BindableBase
{
public ViewModel()
{
_Items = new ObservableCollection<Item>(Enumerable.Range(1, 10)
.Select(x => new Item()
{
Text = string.Format("Item {0}", x),
IsChecked = (x < 4) ? true : false,
}));
foreach (var item in this.Items)
item.PropertyChanged += (s, e) => base.RaisePropertyChanged("Header");
}
public string Header
{
get
{
var array = this.Items
.Where(x => x.IsChecked)
.Select(x => x.Text).ToArray();
if (!array.Any())
return "None";
return string.Join("; ", array);
}
}
ObservableCollection<Item> _Items;
public ObservableCollection<Item> Items { get { return _Items; } }
}
public abstract class BindableBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void SetProperty<T>(ref T storage, T value,
[System.Runtime.CompilerServices.CallerMemberName] String propertyName = null)
{
if (!object.Equals(storage, value))
{
storage = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
protected void RaisePropertyChanged([System.Runtime.CompilerServices.CallerMemberName] String propertyName = null)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
Best of luck!
Your all code is correct, except one thing, that's data binding of CheckBox in ComboBox's DataTemplate. You don't need to use ElementName=thisUC
Incorrect
<CheckBox Content="{Binding Title}" Foreground="Black"
IsChecked="{Binding ElementName=thisUC, Path=IsSelected, Mode=TwoWay}"
Click="CheckBox_Click" />
Correct
<CheckBox Content="{Binding Title}" Foreground="Black" Click="CheckBox_Click"
IsChecked="{Binding Path=IsSelected, Mode=TwoWay}"/>