I'm struggling to bind 2 listviews selected items to my viewmodel.
The idea is to have a view with 2 listviews and 2 buttons in between to move the (multiple) selected items from 1 listview to another (2 way).
The view:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<ListView x:Name="DuamResources" Grid.Column="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<ListView.View>
<GridView>
<GridViewColumn Header="Id" DisplayMemberBinding="{Binding Id}"/>
<GridViewColumn Header="Description" DisplayMemberBinding="{Binding Name}"/>
</GridView>
</ListView.View>
<ListView.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<TextBlock FontWeight="Bold" FontSize="14" Text="{Binding Name}"/>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</ListView.GroupStyle>
</ListView>
<StackPanel Grid.Column="1" Orientation="Vertical" cal:Action.TargetWithoutContext="{Binding SelectedDuamResources}" VerticalAlignment="Center">
<Button Content="<" Width="40" Margin="5" cal:Message.Attach="AddSelectedResources($datacontext)"/>
<Button Content=">" Width="40" Margin="5"/>
</StackPanel>
<ListView x:Name="AvailableDuamResources" Grid.Column="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<ListView.View>
<GridView>
<GridViewColumn Header="Id" DisplayMemberBinding="{Binding Id}"/>
<GridViewColumn Header="Description" DisplayMemberBinding="{Binding Name}"/>
</GridView>
</ListView.View>
<ListView.GroupStyle>
<GroupStyle HidesIfEmpty="True">
<GroupStyle.HeaderTemplate>
<DataTemplate>
<TextBlock FontWeight="Bold" FontSize="14" Text="{Binding Type}"/>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</ListView.GroupStyle>
</ListView>
</Grid>
The associated viewmodel properties:
private ObservableCollection<DuamResourceInfo> availableDuamResources;
public ObservableCollection<DuamResourceInfo> AvailableDuamResources
{
get { return availableDuamResources; }
set { availableDuamResources = value; NotifyOfPropertyChange(() => AvailableDuamResources); }
}
private ObservableCollection<DuamResourceInfo> selectedDuamResources;
public ObservableCollection<DuamResourceInfo> SelectedDuamResources
{
get { return selectedDuamResources; }
set { selectedDuamResources = value; NotifyOfPropertyChange(() => SelectedDuamResources); }
}
private ObservableCollection<DuamResourceInfo> duamResources;
public ObservableCollection<DuamResourceInfo> DuamResources
{
get { return duamResources; }
set { duamResources = value; NotifyOfPropertyChange(() => DuamResources); }
}
The code I want to achieve:
public void AddSelectedResources(IEnumerable<DuamResourceInfo> resources)
{
}
public void RemoveSelectedResources(IEnumerable<DuamResourceInfo> resources)
{
}
I'm using Caliburn Micro for MVVM. I know there are some conventions for binding but I'm not sure they apply to this scenario.
Any help is greatly appreciated!
Have you tried using "x:Bind" instead of "Binding"? For some reason in the past one or the other has worked for me, but not both, depending on the situation.
Related
Good day,
I would like to ask question on how to properly bind the RelayCommand UpdateUserCommand() in toggle button. Please see my code below
<UserControl x:Class="istock.View.UserMasterList"
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:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
xmlns:local="clr-namespace:istock.View"
mc:Ignorable="d"
MinHeight="450" Width="Auto" MinWidth="1024" FontFamily="Segoe UI LIght">
<Grid>
<Border Padding="30 15">
<StackPanel>
<Grid>
<TextBlock Text="User Management" FontSize="20" HorizontalAlignment="Left" Width="Auto" />
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Width="Auto">
<Button x:Name="btnNew" Foreground="#FFFF">
<StackPanel Orientation="Horizontal">
<materialDesign:PackIcon Kind="PlusBoxOutline" Margin="0 5" />
<TextBlock Text="New User" Margin="5 3 5 0" />
</StackPanel>
</Button>
</StackPanel>
</Grid>
<ListView ItemsSource="{Binding Path = UserMasterListProp}">
<ListView.View>
<GridView>
<GridViewColumn Header="ID" Width="auto" DisplayMemberBinding="{Binding Path = Id}"></GridViewColumn>
<GridViewColumn Header="Username" Width="120" DisplayMemberBinding="{Binding Path = Username}"></GridViewColumn>
<GridViewColumn Header="Firstname" Width="120" DisplayMemberBinding="{Binding Path = Firstname}"></GridViewColumn>
<GridViewColumn Header="Middlename" Width="120" DisplayMemberBinding="{Binding Path = Middlename}"></GridViewColumn>
<GridViewColumn Header="Lastname" Width="120" DisplayMemberBinding="{Binding Path = Lastname}"></GridViewColumn>
<GridViewColumn Header="Role" Width="100" DisplayMemberBinding="{Binding Path = Role}"></GridViewColumn>
<GridViewColumn Header="Activated">
<GridViewColumn.CellTemplate>
<DataTemplate>
<ToggleButton x:Name="tbtnActivated" Style="{DynamicResource MaterialDesignSwitchToggleButton}" IsChecked="{Binding Path = Activated}" Command="{Binding Path = UserViewModel.UpdateUserCommand}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Locked">
<GridViewColumn.CellTemplate>
<DataTemplate>
<ToggleButton x:Name="tbtnLocked" Style="{DynamicResource MaterialDesignSwitchToggleButton}" IsChecked="{Binding Path = Locked}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</StackPanel>
</Border>
</Grid>
Here is my Command Code which was initialized and CanExecute() method is always true.
public class RelayCommand : ICommand
{
private Action ExecuteCmd;
public RelayCommand(Action paramAction)
{
this.ExecuteCmd = paramAction;
}
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter)
{
return true;
}
public void Execute(object parameter)
{
this.ExecuteCmd();
}
}
And Here's my code in ViewModel which the UpdateUser() method will be fired
by my UpdateUserCommand()
#region Update Command and Method
private RelayCommand updateUserCommand;
public RelayCommand UpdateUserCommand
{
get { return updateUserCommand; }
}
public void UpdateUser()
{
try
{
var isExist = this.userService.ApplyService_FindByIdOrUsername(UserModel.Id, UserModel.Username);
if(isExist == null)
{
var isUpdated = this.userService.ApplyService_Update(UserModel);
if (isUpdated == true)
{
this.Message = "Update sucesss.";
this.Populate_UserMasterList();
}
}
else
{
this.Message = "Update failed. ID or Username already exist.";
return;
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message.ToString());
return;
}
}
#endregion
Here is the implementation of the Data Context in Code Behind of the User Control
public partial class UserMasterList : UserControl
{
public UserMasterList()
{
InitializeComponent();
UserViewModel userViewModel = new UserViewModel();
this.DataContext = userViewModel;
}
}
Please help. If ever my code is confusing, I am open for question. Thanks
You could bind to a property of the parent UserControl using a {RelativeSource}:
<ToggleButton x:Name="tbtnActivated"
Style="{DynamicResource MaterialDesignSwitchToggleButton}"
IsChecked="{Binding Activated}"
Command="{Binding DataContext.UpdateUserCommand,
RelativeSource={RelativeSource AncestorType=UserControl}}" />
I have a ListView (with an inner ListView) that displays data like this:
I would like to display the inner ListView headers above the grouping like so:
Is it possible to re-position the column headers as shown or simply create some fake headers on the outer ListView?
Here is the XAML code I have so far:
<ListView Name="ListView_GarnishmentCalculations"
ItemsSource="{Binding GarnishedEmployees, UpdateSourceTrigger=PropertyChanged}"
MaxHeight="{Binding ActualHeight,
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ScrollContentPresenter}},
Converter={StaticResource MathConverter}, ConverterParameter=x-220}"
Margin="5,20,10,10"
ScrollViewer.CanContentScroll="True"
ScrollViewer.VerticalScrollBarVisibility="Auto"
Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="4">
<!-- Required for right justifying text in a TextBlock -->
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListView.ItemContainerStyle>
<!-- Group results and show EmpNo, Name and WorkState -->
<ListView.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<Grid ShowGridLines="False">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50" />
<ColumnDefinition Width="175" />
<ColumnDefinition Width="50" />
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Items[0].EmpNo}"
FontWeight="Bold"
Grid.Column="0" />
<TextBlock Text="{Binding Items[0].FullName}"
FontWeight="Bold"
Grid.Column="1" />
<TextBlock Text="{Binding Items[0].WorkState}"
FontWeight="Bold"
Grid.Column="2" />
</Grid>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</ListView.GroupStyle>
<ListView.ItemTemplate>
<DataTemplate>
<!-- Inner ListView of garnishment details -->
<ListView ItemsSource="{Binding Garnishments}">
<ListView.View>
<GridView>
<!-- CaseID -->
<GridViewColumn Width="100">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding CaseNumber, Converter={StaticResource StringIsNullOrEmptyConverter}, ConverterParameter='No Case ID'}"
TextAlignment="Left">
</TextBlock>
</DataTemplate>
</GridViewColumn.CellTemplate>
<GridViewColumn.Header>
<GridViewColumnHeader Content=" Case ID" />
</GridViewColumn.Header>
</GridViewColumn>
<!-- Vendor -->
<GridViewColumn Width="150"
DisplayMemberBinding="{Binding Vendor}">
<GridViewColumn.Header>
<GridViewColumnHeader Content=" Vendor" />
</GridViewColumn.Header>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
This is what I came up with, NOT by my self I used my google fu skills for this.
Credit to this SO post.
So here is what I have for my model:
namespace Model
{
public class Case
{
public int CaseID { get; set; }
public int Vendor { get; set; }
}
}
And now user:
namespace Model
{
public class User
{
public int ID { get; set; }
public string Name { get; set; }
public string State { get; set; }
public List<Case> Cases { get; set; }
}
}
Now in my MainViewModel:
using Model;
using System.Collections.Generic;
namespace VM
{
public class MainViewModel : BaseViewModel
{
public MainViewModel()
{
Users = new List<User>();
for (int i = 0; i < 20000; i++)
{
Users.Add(new User
{
ID = i,
Name = $"John the {i + 1}",
State = i % 2 == 0 ? "CA" : "IL",
Cases = new List<Case>() { new Case { CaseID = (i + 1) * 10, Vendor = ((i + 1) * 10) - 2 }, new Case { CaseID = (i + 1) * 10, Vendor = ((i + 1) * 10) - 2 } }
});
}
}
private List<User> users;
public List<User> Users
{
get { return users; }
set { users = value; OnPropertyChanged(); }
}
}
}
On to the View:
<Window x:Class="SO_App.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:vm="clr-namespace:VM;assembly=VM"
xmlns:model="clr-namespace:Model;assembly=Model"
xmlns:local="clr-namespace:SO_App"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<vm:MainViewModel/>
</Window.DataContext>
<Window.Resources>
<CollectionViewSource Source="{Binding Users}" x:Key="Users"/>
</Window.Resources>
<Grid>
<ListView>
<ListView.ItemsSource>
<CompositeCollection>
<CollectionContainer Collection="{Binding Source={StaticResource Users}}"/>
</CompositeCollection>
</ListView.ItemsSource>
<ListView.View>
<GridView>
<GridViewColumn Header="Case ID" Width="100"/>
<GridViewColumn Header="Vendor" Width="100"/>
</GridView>
</ListView.View>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<ContentPresenter/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemTemplate>
<DataTemplate>
<DataTemplate.Resources>
<Style TargetType="TextBlock">
<Setter Property="FontWeight" Value="Bold"/>
</Style>
</DataTemplate.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock Text="{Binding ID}" MinWidth="50"/>
<TextBlock Text="{Binding Name}" MinWidth="250" Grid.Column="1"/>
<TextBlock Text="{Binding State}" MinWidth="50" Grid.Column="2"/>
<ListView Grid.Row="1" ItemsSource="{Binding Cases}" Grid.ColumnSpan="3">
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding CaseID}" MinWidth="100"/>
<TextBlock Text="{Binding Vendor}" MinWidth="100" Grid.Column="1"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
Caveat:
You will need to handle the scroll event on the inner ListView so it doesn't swallow the mouse scroll.
P.S.
this is the BaseViewModel implementation:
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace VM
{
public class BaseViewModel : INotifyPropertyChanged
{
#region INPC
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string prop = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(prop));
}
#endregion
}
}
Which then produces this as a result:
How to check all checkboxes on listview?
Listview xaml:
<ListView x:Name="List" HorizontalAlignment="Left" Height="559" Margin="10,10,0,0" VerticalAlignment="Top" Width="607" Grid.ColumnSpan="2" ScrollViewer.HorizontalScrollBarVisibility="Visible" ScrollViewer.VerticalScrollBarVisibility="Auto" IsMouseCapturedChanged="List_IsMouseCapturedChanged">
<ListView.ContextMenu>
<ContextMenu>
<MenuItem Name="Dodaj" Header="Dodaj" Click="DodajUtwor_Click"/>
<MenuItem Name="Usuń" Header="Usuń Zaznaczone" Click="UsuńUtwór_Click"/>
<MenuItem Name="Clear" Header="Wyczyść Listę" Click="ClearList_Click"/>
<MenuItem Name="Check" Header="Zaznacz Wszystkie" Click="SelectAll_Click"/>
</ContextMenu>
</ListView.ContextMenu>
<ListView.View>
<GridView>
<GridViewColumn Width="30" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox Name="CheckBox" Padding="10" IsChecked="{Binding IsChecked}"/>
<TextBlock Text="{Binding Data}"/>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Nazwa Pliku" Width="100" DisplayMemberBinding="{Binding Nazwa}" />
<GridViewColumn Header="Tytuł" Width="100" DisplayMemberBinding="{Binding Tytuł}"/>
<GridViewColumn Header="Wykonawca" Width="100" DisplayMemberBinding="{Binding Autor}"/>
<GridViewColumn Header="Album" Width="100" DisplayMemberBinding="{Binding Album}"/>
<GridViewColumn Header="Nr" Width="100" DisplayMemberBinding="{Binding Nr}"/>
<GridViewColumn Header="Rok" Width="100" DisplayMemberBinding="{Binding Rok}"/>
<GridViewColumn Header="Gatunek" Width="100" DisplayMemberBinding="{Binding Gatunek}"/>
<GridViewColumn Header="Komantarze" Width="100" DisplayMemberBinding="{Binding Komentarze}"/>
<GridViewColumn Header="Okładka" Width="100" DisplayMemberBinding="{Binding Okładka}"/>
<GridViewColumn Header="Lokalizacja" Width="100" DisplayMemberBinding="{Binding Lokalizacja}"/>
</GridView>
</ListView.View>
</ListView>
Code:
private void Button_Click(object sender, RoutedEventArgs e)
{
MyItem selectedItem = (MyItem)this.List.SelectedItem;
selectedItem.IsChedked = true;
List.Items.Refresh();
}
Don't work.
I want to change all checkboxes on true in a listviev. I dont know how to use loop foreach. And how to delete items witch checkboxes on true?
I guess you might be looking for a checkbox in the header column and select all grid items when header checkbox got selected.
We can do this having simple child/sub viewmodels. Below code might help/solve your problem.
Xaml/View code -
<GridViewDataColumn DataMemberBinding="{Binding IsChecked}" >
<GridViewDataColumn.Header >
<CheckBox HorizontalAlignment="Left" IsChecked="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type GridViewDataControl}}
,Path=DataContext.CheckAll}"
Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type GridViewDataControl}}
,Path=DataContext.CheckAllCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=IsChecked}" />
</GridViewDataColumn.Header>
<GridViewDataColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox Name="CheckBox" Padding="10" IsChecked="{Binding IsChecked}" Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type GridViewDataControl}}
,Path=DataContext.ItemCommand}" CommandParameter="{Binding}"/>
<TextBlock Text=y"{Binding Data}"/>
</StackPanel>
</DataTemplate>
</GridViewDataColumn.CellTemplate>
</GridViewDataColumn>
and you can have your view models as explained below.
class MainViewModel : Common.UserInterface.ViewModelBase, IDisposable
{
public MainViewModel()
{
CheckAllCommand = new DelegateCommand<bool>(OnCheckAllExecuted);
}
void OnCheckAllExecuted(bool status)
{
// Your logic goes here (if any)
CheckAll = items.All(item => item.IsChecked);
}
public DelegateCommand<ItemViewModel> ItemCommand { get; private set; }
private bool checkAll;
private ObservableCollection<ItemViewModel> items;
public DelegateCommand<bool> CheckAllCommand
{
get;
private set;
}
public ObservableCollection<ItemViewModel> Items
{
get
{
return items;
}
set
{
items = value;
OnPropertyChanged(() => Items);
}
}
public bool CheckAll
{
get { return checkAll; }
set
{
checkAll = value;
OnPropertyChanged(() => CheckAll);
}
}
}
class ItemViewModel : ViewModelBase
{
public bool ischecked;
public string data;
public new bool IsChecked
{
get { return isChecked; }
set
{
isChecked = value;
OnPropertyChanged("IsChecked");
}
}
public string Data
{
get { return data; }
set
{
data = value;
OnPropertyChanged("Data");
}
}
}
I have an application where I present a Form. The form contains multiple Radio Buttons and they are displayed 1 next to each other. I need to load the colors from the list and create a radio button for each color, then when the users selects a color I want to grab the "SelectedItem" from the control. I know you can do that easily with a list but how do I do that when I need to place the controls next to each other ??
Code :
<Grid>
<StackPanel VerticalAlignment="Top">
<GroupBox Name="CheckBoxes" Margin="5" >
<StackPanel Name="wrpCheckBoxes" DataContext="{Binding ListParts}">
<RadioButton Name="chkRed" Content="{Binding Description}" Visibility="{Binding DataBindingModel.ColorRed}" Tag="{Binding ID}" />
<RadioButton Name="chkGreen" Content="Green" Visibility="{Binding DataBindingModel.ColorGreen}" />
<RadioButton Name="chkBlue" Content="Blue" Visibility="{Binding DataBindingModel.ColorBlue}" />
<RadioButton Name="chkGray" Content="Gray" Visibility="{Binding DataBindingModel.ColorGray}" />
<RadioButton Name="chkYellow" Content="Yellow" Visibility="{Binding DataBindingModel.ColorYellow}" />
<RadioButton Name="chkBlack" Content="Black" Visibility="{Binding DataBindingModel.ColorBlack}" />
</StackPanel>
</GroupBox>
</StackPanel>
<StackPanel VerticalAlignment="Bottom" Margin="5">
<ListView x:Name="listBoxItems" BorderThickness="1" ItemsSource="{Binding ListParts}">
<ListView.View>
<GridView>
<GridViewColumn Header="Index" DisplayMemberBinding="{Binding Index}" />
<GridViewColumn Header="Description" DisplayMemberBinding="{Binding Description}" />
<GridViewColumn Header="Price" DisplayMemberBinding="{Binding Price}" />
</GridView>
</ListView.View>
</ListView>
<!--<ListBox x:Name="listBoxItems" ItemsSource="{Binding ListParts}">
<ListBox.ItemTemplate>
<DataTemplate>
<RadioButton GroupName="rbList" Tag="{Binding}" Content="{Binding Description}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>-->
</StackPanel>
</Grid>
Remark : the ListParts is just a list containing the colors, also this view is attached to a viewModel
ViewModel :
public class DataBindingViewModel : ViewModelBase
{
#region Private Fields
private DataBindingModel _DataBindingModel;
private List<PartsModel> _ListParts;
private string _Description1;
private int _TagID;
#endregion
#region Properties
public DataBindingModel DataBindingModel
{
get { return this._DataBindingModel; }
set
{
if (this._DataBindingModel == value)
return;
this._DataBindingModel = value;
OnPropertyChanged("DataBindingModel");
}
}
public List<PartsModel> ListParts
{
get { return this._ListParts; }
set
{
if (this._ListParts == value)
return;
this._ListParts = value;
OnPropertyChanged("ListParts");
}
}
public string Description1
{
get { return this._Description1; }
set
{
if (this._Description1 == value)
return;
this._Description1 = value;
OnPropertyChanged("Description1");
}
}
public int TagID
{
get { return this._TagID; }
set
{
if (this._TagID == value)
return;
this._TagID = value;
OnPropertyChanged("TagID");
}
}
#endregion
public DataBindingViewModel(DataBindingModel DataBinding)
{
this.DataBindingModel = DataBinding;
this.ListParts = Common.GetData();
}
}
And the Common Class is just laoding the Data :
public static class Common
{
public static List<PartsModel> GetData()
{
List<PartsModel> listParts = new List<PartsModel>();
listParts.Add(new PartsModel(1, "1234561", "Color Red", Convert.ToDecimal(15.99)));
listParts.Add(new PartsModel(2, "1234562", "Color Green", Convert.ToDecimal(17.00)));
listParts.Add(new PartsModel(3, "1234563", "Color Blue", Convert.ToDecimal(12.95)));
listParts.Add(new PartsModel(4, "1234564", "Color Gray", Convert.ToDecimal(9.95)));
listParts.Add(new PartsModel(5, "1234565", "Color Yellow", Convert.ToDecimal(10.55)));
listParts.Add(new PartsModel(6, "1234566", "Color Black", Convert.ToDecimal(99.99)));
return listParts;
}
}
How can I display the members of the ListParts on each of my radio button, without the use of a ListView ?
Let me know if you guys need more info and thanks for the answers
You can use ListBox.ItemsPanel to specify the type of panel you want - probably <StackPanel Orientation="Horizontal/>.
<ListBox>
<ListBox.ItemTemplate>
<DataTemplate>
<RadioButton GroupName="rbList" Tag="{Binding}" Content="{Binding Description}" />
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"
VerticalAlignment="Center"
HorizontalAlignment="Center"/>
</ItemsPanelTemplate>
<ListBox.ItemsPanel>
<ListBox>
See the examples in the ItemsPanel property documentation on MSDN for more information.
Goal:
Change the listview's background color to light colored grey if the current time pass the appointment time. For instance, the time is 10.15 and you have background color for the 09.30 - 10.30 and 10.00 - 11.00.
Problem:
I do not know what syntax I need to use in order achieve the goal. I also reviews some advice from different website and their advice is not enough.
Information:
- I'am using Nhibernate to retrive the data.
- This is a usercontrol.
private void DisplayActivityBasedonSelectedDate()
{
DateTime aa = (DateTime) cdate_left.SelectedDate;
lvw_aktivitet.DataContext = _myNhibernateDataProvider.RetrieveAllActivitetBasedOnDate(aa);
DisplayDateInTheLabel();
}
public IList<Aktivitet_data> RetrieveAllActivitetBasedOnDate(DateTime pDatum)
{
return _session.GetNamedQuery("sp_retrieveAllActivitetBasedOnDate").SetDateTime("Datum", pDatum)
.SetResultTransformer(Transformers.AliasToBean(typeof(Aktivitet_data))).List<Aktivitet_data>();
}
public class Aktivitet_data
{
public string PK_Aktivitet_schema { get; set; }
public string Datum { get; set; }
public string Tid { get; set; }
public string Aktivitet { get; set; }
public string Total_platser { get; set; }
public string Bokade { get; set; }
public string Drop_in { get; set; }
}
XAML:
<UserControl x:Class="usercontrol_bokning.UserControl1"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid Background="White" Margin="-127,0,-777,-316">
<ListView x:Name="lvw_aktivitet" HorizontalAlignment="Left" ItemsSource="{Binding}" SelectionMode="Single" Height="337" Margin="217,147,0,0" VerticalAlignment="Top" Width="365" SelectionChanged="lvw_aktivitet_SelectionChanged">
<ListView.View>
<GridView>
<GridViewColumn Header="Tid" Width="auto" DisplayMemberBinding="{Binding Path=Tid}" TextBlock.TextAlignment="Left" />
<GridViewColumn Header="Aktivitet" Width="auto" DisplayMemberBinding="{Binding Path=Aktivitet}" TextBlock.TextAlignment="Left" />
<GridViewColumn Header="Total platser" Width="auto" DisplayMemberBinding="{Binding Path=Total_platser}" TextBlock.TextAlignment="Center" />
<GridViewColumn Header="Bokade" Width="auto" DisplayMemberBinding="{Binding Path=Bokade}" TextBlock.TextAlignment="Center" />
<GridViewColumn Header="Drop in" Width="auto" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock TextAlignment="Right" Block.TextAlignment="Right" Text="{Binding Path=Drop_in}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
<Calendar x:Name="cdate_left" HorizontalAlignment="Left" Margin="10,147,0,0" VerticalAlignment="Top" SelectedDatesChanged="cdate_left_SelectedDatesChanged"/>
<Button x:Name="btn_idag" Content="Idag" HorizontalAlignment="Left" Margin="217,122,0,0" VerticalAlignment="Top" Width="75" Click="btn_idag_Click"/>
<Button x:Name="arrow__left" Click="Arrow_left_Click" Background="Transparent">
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<Image x:Name="bild_arrow_left" HorizontalAlignment="Left" Height="22" Margin="302,121,0,0" VerticalAlignment="Top" Width="22" Source="C:\Users\Administrator\Downloads\left.jpg" />
</ControlTemplate>
</Button.Template>
</Button>
<Button x:Name="arrow__right" Click="Arrow_right_Click" Background="Transparent" >
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<Image x:Name="bild_arrow_right" HorizontalAlignment="Left" Height="22" Margin="326,121,0,0" VerticalAlignment="Top" Width="22" Source="C:\Users\Administrator\Downloads\right.jpg"/>
</ControlTemplate>
</Button.Template>
</Button>
<Label x:Name="lbl_DisplayDateBySelection" Content="" HorizontalAlignment="Left" Margin="396,119,0,0" VerticalAlignment="Top"/>
<TabControl HorizontalAlignment="Left" Height="420" Margin="603,75,0,0" VerticalAlignment="Top" Width="589">
<TabItem Header="Bokade">
<Grid Background="#FFE5E5E5">
<ListView x:Name="lvw_bokade" HorizontalAlignment="Left" Height="335" Margin="10,46,0,0" VerticalAlignment="Top" Width="561">
<ListView.View>
<GridView>
<GridViewColumn/>
</GridView>
</ListView.View>
</ListView>
<Button Content="Ny plats" HorizontalAlignment="Left" Margin="10,21,0,0" VerticalAlignment="Top" Width="75"/>
</Grid>
</TabItem>
<TabItem Header="Avbokade">
<Grid Background="#FFE5E5E5" Margin="0,1,0,-1"/>
</TabItem>
<TabItem Header="Avregistrerade" HorizontalAlignment="Left" Height="20" VerticalAlignment="Top" Width="55">
<Grid Background="#FFE5E5E5"/>
</TabItem>
</TabControl>
<Label x:Name="lbl_instruktor" Content="Instruktör: " HorizontalAlignment="Left" Margin="1009,122,0,0" VerticalAlignment="Top"/>
</Grid>
</UserControl>
If you want it to update the items automatically you'll have to use some sort of a timer.
I used DispatcherTimer set globally in the UserControl
public DispatcherTimer dispatcher;
On Loaded / Ctor
dispatcher = new DispatcherTimer();
dispatcher.Tick += CheckTime;
dispatcher.Interval = new TimeSpan(0, 0, 5); //Change the interval to whatever suits you best
dispatcher.Start();
and the method to check your data:
private void CheckTime(object sender, EventArgs e)
{
ListBoxItem lbitem;
foreach (var item in lvw_aktivitet.Items)
{
if ((item as Aktivitet_data).Tid > DateTime.Now)
{
lbitem = (ListBoxItem)lvw_aktivitet.ItemContainerGenerator.ContainerFromItem(item);
lbitem.Background = new SolidColorBrush(Colors.Gray);
}
}
}