I have the following xaml in my WPF project
<ItemsControl ItemsSource="{Binding ParentJob.JobSamples}" Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="2">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Expander ExpandDirection="Down" IsExpanded="{Binding Path=IsExpanded}">
<Expander.Header>
<BulletDecorator>
<Label Content="{Binding Identifier}"></Label>
</BulletDecorator>
</Expander.Header>
<ItemsControl ItemsSource="{Binding Path=SelectedAnalyticalTests}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<CheckBox Margin="4" Content="{Binding AnalyticalTestName}" Foreground="{DynamicResource InputFontColor}" Checked="{Binding IsSelected}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Expander>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
My objects are set up such that ParentJob has an ObservableCollection of JobSample objects
and JobSample has an ObservableCollection of AnalyticalTest objects called SelectedAnalyticalTests
If I load up a ParentJob that has JobSamples that don't contain any SelectedAnalyticalTests I get each of the JobSamples listed correctly.
If I attempt to load a ParentJob that has JobSamples that do have SelectedAnalyticalTests I get the following error:-
'Disptacher processing has been suspended, but messages are still being processed'
Has this got to do with my IsExpanded attribute? How can I get around this?
The ViewModel is broken up into 3 partial classes as follows:-
public partial class JobRepeatViewModel : ViewModelBase
{
public JobRepeatViewModel(MainWindowViewModel mainWindowVM)
{
this.MainWindowViewModel = mainWindowVM;
try
{
DependencyObject dep = new DependencyObject();
if (DesignerProperties.GetIsInDesignMode(dep))
{
return;
}
this.DA = new ACSDataAccess();
LoadDropdowns();
}
catch (Exception ex)
{
MessageBox.Show(ex.InnerException.ToString());
}
}
private void LoadDropdowns()
{
this.StaffMembers = this.DA.GetStaffMembers();
}
}
public partial class JobRepeatViewModel : ViewModelBase
{
public void OnJobRepeatAnalysisSavePopUpClicked(object sender, RoutedEventArgs e)
{
if (this.OnSavePopUpClicked != null)
{
this.OnSavePopUpClicked(sender);
}
}
public void OnJobRepeatAnalysisCancelPopUpClicked(object sender, RoutedEventArgs e)
{
if (this.OnCancelPopUpClicked != null)
{
this.OnCancelPopUpClicked(sender);
}
}
public void Execute_SaveRepeatJobClick()
{
}
}
public partial class JobRepeatViewModel : ViewModelBase
{
public event JobRepeatAnalysisCancelPopupClicked_Event OnCancelPopUpClicked;
public delegate void JobRepeatAnalysisCancelPopupClicked_Event(object sender);
public event JobRepeatAnalysisSavePopupClicked_Event OnSavePopUpClicked;
public delegate void JobRepeatAnalysisSavePopupClicked_Event(object sender);
public MainWindowViewModel MainWindowViewModel
{
get { return Get(() => MainWindowViewModel); }
set { Set(() => MainWindowViewModel, value); }
}
public Job ParentJob
{
get { return Get(() => ParentJob); }
set { Set(() => ParentJob, value); }
}
public ACSDataAccess DA
{
get { return Get(() => DA); }
set { Set(() => DA, value); }
}
public ObservableCollection<Staff> StaffMembers
{
get { return Get(() => StaffMembers); }
set { Set(() => StaffMembers, value); }
}
}
Related
I have the following "element":
public class ReportElementViewModel
{
public string Name
{
get { return _name; }
set
{
if (_name != value)
{
_name = value;
RaisePropertyChanged("Name");
}
}
}
public bool IsChecked
{
get { return _isChecked; }
set
{
if (_isChecked != value)
{
_isChecked = value;
RaisePropertyChanged("IsChecked");
}
}
}
}
My ViewModel contains many ReportElementViewModels:
public abstract class ReportViewModel<TPrimaryModel> : SharedViewModel
where TPrimaryModel : Model, new()
{
public ObservableCollection<ReportElementViewModel> ReportElementViewModels
{
get { return _reportElementViewModels; }
set
{
if (_reportElementViewModels != value)
{
_reportElementViewModels = value;
RaisePropertyChanged("ReportElementViewModels");
}
}
}
}
I removed the members to reduce code complexity but they are implemented correctly.
In my view I want to show all ReportElementViewModels by showing their Name and IsChecked (checkbox).
I thought the ItemsControl would be the right "tool", however it doesn't work (nothing is shown):
<ItemsControl ItemsSource="{Binding ReportElemetViewModels}" Height="auto" VerticalAlignment="Top" Grid.Row="1">
<ItemsControl.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding Name}" IsChecked="{Binding IsChecked}" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="269"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
You have made a spelling mistake in your Binding.
ItemsSource="{Binding ReportElemetViewModels}"
Should be:
ItemsSource="{Binding ReportElementViewModels}"
My Problem, I can't remove list in my listbox from browser, but when i debug in my code data list has removed.
View Model
private List<Customer> _listCustomer = new List<Customer>();
public List<Customers> ListCustomers
{
get { return _listCustomers; }
set { _listCustomers = value; OnPropertyChanged("ListCustomers"); }
}
private ICommand _removeCommand;
public ICommand RemoveCommand
{
get { return _removeCommand ?? (_removeCommand = new RelayCommand(param => ButtonRemoveCustomer(), null)); }
set { OnPropertyChanged("ListCustomers"); }
}
private void ButtonRemoveCustomer()
{
ListCustomers.Remove(this.SelectCustomer);
this.SelectCustomer = null;
}
My View
<Intersoft:UXListBox x:Name="CustData" Width="200" MaxHeight="500" SelectedItem="{Binding SelectCustomer, Mode=TwoWay}"
ItemsSource="{Binding ListCustomers, Mode=TwoWay}" ItemTemplate="{StaticResource DataListTemplate}"
VerticalScrollBarVisibility ="Visible" HorizontalScrollBarVisibility="Visible"/>
<Intersoft:UXButton Content="Remove List" Command="{Binding RemoveCommand, Mode=TwoWay}" Width="100"/>
How to fix my code ??
private void ButtonRemoveCustomer()
{
ListCustomers.Remove(this.SelectCustomer);
OnPropertyChanged("ListCustomers");
this.SelectCustomer=null;
}
I want to pass selected items of listview checkbox in view model, later I will use further process to store in database.
Code in the FormWeek.xaml as
<Window.DataContext>
<Binding Source="{StaticResource Locator}" Path="TaskExecDefModel"></Binding>
</Window.DataContext>
<Window.Resources>
<ResourceDictionary>
<DataTemplate x:Key="ItemDataTemplate">
<CheckBox
x:Name="checkbox"
Content="{Binding}" Command="{Binding CheckBoxCommand}" CommandParameter="{Binding ElementName=checkedListView, Path=SelectedItems}"
IsChecked="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}, Path=IsSelected}" />
</DataTemplate>
</ResourceDictionary>
<StackPanel Grid.Column="1" Grid.Row="3" Margin="5">
<CheckBox x:Name="selectAll" Content="Select all" Click="OnSelectAllChanged"/>
<ListView x:Name="checkedListView" SelectionMode="Multiple" ItemsSource="{Binding CollectionOfDays}" DataContext="{Binding}"
ItemTemplate="{StaticResource ItemDataTemplate}" SelectedValue="WorkingDay"
CheckBox.Unchecked="OnUncheckItem" SelectionChanged="SelectDays" SelectedItem="{Binding SelectedItems}">
</ListView>
</StackPanel>
Code in FormWeek.xaml.cs
private void SelectDays(object sender, SelectionChangedEventArgs e)
{
(this.DataContext as TaskExecDefinitionViewModel).OnCheckBoxCommand(checkedListView.SelectedItems,true);
}
My View Model TaskWeek.cs as follows
//Declaration
private RelayCommand<object> _checkBoxCommand;
public ObservableCollection<string> CollectionOfDays { get; set; }
public ObservableCollection<string> SelectedItems { get; set; }
public RelayCommand<object> CheckBoxCommand
{
get
{
if (_checkBoxCommand == null)
{
_checkBoxCommand = new RelayCommand<object>((args) => OnCheckBoxCommand(args,true));
// _checkBoxCommand = new RelayCommand(() => OnCheckBoxCommand(object args));
}
return _checkBoxCommand;
}
}
//Constructor
CollectionOfDays = new ObservableCollection<string>();
//Method
private void GetWeekDays()
{
try
{
Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() =>
{
CollectionOfDays.Add("Saturday");
CollectionOfDays.Add("Sunday");
CollectionOfDays.Add("Monday");
CollectionOfDays.Add("Tuesday");
CollectionOfDays.Add("Wednesday");
CollectionOfDays.Add("Thursday");
CollectionOfDays.Add("Friday");
}));
}
catch(Exception Ex)
{
MessageBox.Show(Ex.Message, "TaskWeek:GetWeekDays");
}
}
public void OnCheckBoxCommand(object obj, bool _direction)
{
try
{
if (SelectedItems == null)
SelectedItems = new ObservableCollection<string>();
if (obj != null)
{
SelectedItems.Clear();
StringBuilder items = new StringBuilder();
if (_direction)
{
foreach (string item in CollectionOfDays)
{
items.AppendFormat(item + ",");
}
}
MessageBox.Show(items.ToString());
}
else
SelectedItems.Clear();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "TaskDefinition:OnCheckBoxCommand");
}
}
And below is button click commond to save the data.
<Button Grid.Column="2" Grid.Row="2" Content="Save" HorizontalAlignment="Center" VerticalAlignment="Bottom"
Command="{Binding InsertExecDefCommand}" Margin="5" >
Now my requirement is to pass selected listview items to view model through command object. I had done this using following code in FormWeek.xam.cs through SelectionChanged event as
private void OnSelectedItems(object sender, RoutedEventArgs e)
{
StringBuilder items = new StringBuilder();
foreach (string item in checkedListView.SelectedItems)
{
items.AppendFormat(item + ",");
}
string AllDays= items.ToString();
}
But please let me know how to achieve this logic through MVVM. How to get selecteditems in my view model TaskWeek.cs
After R&D and google i had done chages through RelayCommand. In OnCheckBoxCommand method foreach statement is wrong it is passing all days. I want to pass only selected listview item. Please suggest me what is wrong in OnCheckBoxCommand method.
Here are my findings;
use this code in code behind of FormWeek.xaml:
private void SelectDays(object sender, SelectionChangedEventArgs e)
{
(this.DataContext as TaskExecDefinitionViewModel).OnCheckBoxCommand(checkedListView.SelectedItems,true);
}
And in 'OnCheckBoxCommand': -
public void OnCheckBoxCommand(object obj, bool _direction)
{
try
{
if (SelectedItems == null) SelectedItems = new ObservableCollection<string>();
if (obj != null)
{
SelectedItems.Clear();
var _list = ((IList)obj).Cast<string>().ToList();
if (_direction)
{
_list.ForEach(item => SelectedItems.Add(item));
}
}
else
SelectedItems.Clear();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "TaskDefinition:OnCheckBoxCommand");
}
}
Have a nice day man.....keep going.
Hi try something like this
<StackPanel Grid.Column="1" Grid.Row="3" Margin="5">
<CheckBox x:Name="selectAll" Content="Select all" Command="{Binding CheckBoxCommand}" CommandParameter="{Binding IsChecked, RelativeSource={RelativeSource Mode=Self}}"/>
<ListView x:Name="checkedListView" SelectionMode="Extended" ItemsSource="{Binding CollectionOfDays}" SelectedItem="{Binding SelectedItems}"/>
</StackPanel>
public class MainViewModel
{
public MainViewModel()
{
CollectionOfDays = new ObservableCollection<string>();
SelectedItems = new ObservableCollection<string>();
CollectionOfDays.Add("Saturday");
CollectionOfDays.Add("Sunday");
CollectionOfDays.Add("Monday");
CollectionOfDays.Add("Tuesday");
CollectionOfDays.Add("Wednesday");
CollectionOfDays.Add("Thursday");
CollectionOfDays.Add("Friday");
}
private CommandHandler _checkBoxCommand;
public CommandHandler CheckBoxCommand
{
get
{
return _checkBoxCommand ?? (_checkBoxCommand=new CommandHandler((param)=>OnCheckBoxCommand(param)));
}
}
public ObservableCollection<string> CollectionOfDays { get; set; }
public ObservableCollection<string> SelectedItems {get;set;}
private void OnCheckBoxCommand(object obj)
{
if (obj is bool)
{
if (SelectedItems == null)
SelectedItems = new ObservableCollection<string>();
if ((bool)obj)
{
SelectedItems.Clear();
foreach (var item in CollectionOfDays)
{
SelectedItems.Add(item);
}
}
else
SelectedItems.Clear();
}
}
}
public MainWindow()
{
InitializeComponent();
DataContext = new MainViewModel();
}
I hope this will give you an idea.
what am i missing in the implementation of the treeview ??
this is what is see:
View:
<TreeView Style="{StaticResource ExpandAllStyle}" ItemsSource="{Binding Titles}">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type vm:TitleViewModel}">
<Grid ShowGridLines="False">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Path=DisplayedStartTime}" Margin="0,0,10,0" />
<TextBlock Grid.Column="1" Text="" />
<TextBlock Grid.Column="2" Text="{Binding Path=Text}" />
</Grid>
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
my Repository:
public class TitleRepository : INotifyPropertyChanged
{
#region Titles
private ObservableCollectionEx<Title> _titles;
public ObservableCollectionEx<Title> Titles
{
get { return _titles; }
set
{
_titles = value;
OnPropertyChanged(new PropertyChangedEventArgs("Titles"));
}
}
#endregion
#region Current Title
private Title _currentTitle;
public Title CurrentTitle
{
get { return _currentTitle; }
set { _currentTitle = value; }
}
#endregion
public TitleRepository()
{
_titles = new ObservableCollectionEx<Title>();
_titles.Add(new Title("This is a title test"));
}
and my viewModel:
MainTreeViewModel:
public class MainTreeViewModel : ViewModelBase
{
private readonly TitleRepository _titleRepository;
private ObservableCollection<TitleViewModel> _titles;
public ObservableCollection<TitleViewModel> Titles
{
get { return _titles; }
set { _titles = value; }
}
public MainTreeViewModel()
{
_titleRepository = ((App)Application.Current).TitleRepository;
_titleRepository.Titles.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Titles_CollectionChanged);
_titles = new ObservableCollection<TitleViewModel>(
(from t in _titleRepository.Titles
select new TitleViewModel(t)).ToList());
}
void Titles_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
_titles.Add(new TitleViewModel(_titleRepository.Titles[_titleRepository.Titles.Count - 1]));
}
TitleViewModel:
public class TitleViewModel : ViewModelBase
{
readonly Title _title;
#region Displayed Start Time
public string DisplayedStartTime
{
get { return _title.DisplayedStartTime; }
}
#endregion
#region Text
public string Text
{
get { return _title.Text; }
}
#endregion
public TitleViewModel(Title title)
{
_title = title;
}
i bind the view model in the View.cs:
MainTreeViewModel vm = new MainTreeViewModel();
this.DataContext = vm;
i expect to see the title "This is a tile test" that i populated in the repository.
and it's a dynamic tree.
srry found the problem...
another binding in the MainWindow to the dataContext.
the MVVM pattern is valid and working.
Is anyone aware of a free or commercial WPF control that would do something like this:
X character per box, and auto-tabbing to the next box as you complete each box? Similar to the way that license keys are entered for Microsoft products.
I don't think it would be particularly hard to do from scratch, but I'd like to avoid reinventing the wheel if a good example of this already exists.
WPF provides all you need except auto-tabbing to the next control. A behavior can provide that functionality and the result looks like this:
<Grid>
<Grid.Resources>
<local:KeyTextCollection x:Key="keys"/>
</Grid.Resources>
<StackPanel>
<ItemsControl ItemsSource="{StaticResource keys}" Focusable="False">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" Background="AliceBlue"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding Text}" Margin="5" MaxLength="4" Width="40">
<i:Interaction.Behaviors>
<local:TextBoxBehavior AutoTab="True" SelectOnFocus="True"/>
</i:Interaction.Behaviors>
</TextBox>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</Grid>
Here are the KeyText and KeyTextCollection classes (adjust to taste):
class KeyText
{
public string Text { get; set; }
}
class KeyTextCollection : List<KeyText>
{
public KeyTextCollection()
{
for (int i = 0; i < 4; i++) Add(new KeyText { Text = "" });
}
}
And here is the behavior that implements auto-tab and select-on-focus:
public class TextBoxBehavior : Behavior<TextBox>
{
public bool SelectOnFocus
{
get { return (bool)GetValue(SelectOnFocusProperty); }
set { SetValue(SelectOnFocusProperty, value); }
}
public static readonly DependencyProperty SelectOnFocusProperty =
DependencyProperty.Register("SelectOnFocus", typeof(bool), typeof(TextBoxBehavior), new UIPropertyMetadata(false));
public bool AutoTab
{
get { return (bool)GetValue(AutoTabProperty); }
set { SetValue(AutoTabProperty, value); }
}
public static readonly DependencyProperty AutoTabProperty =
DependencyProperty.Register("AutoTab", typeof(bool), typeof(TextBoxBase), new UIPropertyMetadata(false));
protected override void OnAttached()
{
AssociatedObject.PreviewGotKeyboardFocus += (s, e) =>
{
if (SelectOnFocus)
{
Action action = () => AssociatedObject.SelectAll();
AssociatedObject.Dispatcher.BeginInvoke(action, DispatcherPriority.ContextIdle);
}
};
AssociatedObject.TextChanged += (s, e) =>
{
if (AutoTab)
{
if (AssociatedObject.Text.Length == AssociatedObject.MaxLength &&
AssociatedObject.SelectionStart == AssociatedObject.MaxLength)
{
AssociatedObject.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
}
}
};
}
}
If you are not familiar with behaviors, Install the Expression Blend 4 SDK and add this namespaces:
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
and add System.Windows.Interactivity to your project.