I am brand new to WPF and am trying to implement some sort of table (or I don't know how to implement it if not a table), that has its fields selectable (like in the this example - click the Open App icon and then File->New Project and if you click a table field you will see it coloring blue or going back white) and after they have been selected, to send some signals through the USB port with each columns fields.
Now my question is: How could I implement this table (or whatever)?
And another question would be: How could I browse the array (or whatever I would use to memorize states of the fields)?
Thank you in advance for everybody.
Simple example :D
Set a breakpoint for the Check status button clicked event so you can check which buttons are ticked or not.
Code:
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;
namespace WpfApplication1
{
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
Loaded += MainWindow_Loaded;
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
var clickableGrid = new ClickableGrid(3, 3);
DataContext = clickableGrid;
}
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
var clickableGrid = (ClickableGrid) DataContext;
ObservableCollection<MyItem> observableCollection = clickableGrid.MyItems;
}
}
public class ClickableGrid : INotifyPropertyChanged
{
private int _columns;
private ObservableCollection<MyItem> _myItems;
private int _rows;
public ClickableGrid(int columns, int rows)
{
Columns = columns;
Rows = rows;
UpdateArray();
}
public ObservableCollection<MyItem> MyItems
{
get { return _myItems; }
set
{
_myItems = value;
OnPropertyChanged();
}
}
public int Columns
{
get { return _columns; }
set
{
_columns = value;
OnPropertyChanged();
}
}
public int Rows
{
get { return _rows; }
set
{
_rows = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void UpdateArray()
{
int columns = Columns;
int rows = Rows;
if (columns <= 0) columns = 1;
if (rows <= 0) rows = 1;
var observableCollection = new ObservableCollection<MyItem>();
for (int i = 0; i < columns*rows; i++)
{
observableCollection.Add(new MyItem());
}
MyItems = observableCollection;
}
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
public class MyItem : INotifyPropertyChanged
{
private bool _isTicked;
public bool IsTicked
{
get { return _isTicked; }
set
{
_isTicked = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
public override string ToString()
{
return string.Format("IsTicked: {0}", _isTicked);
}
}
}
XAML:
<Window x:Class="WpfApplication1.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:wpfApplication1="clr-namespace:WpfApplication1"
Title="MainWindow"
Width="525"
Height="350"
mc:Ignorable="d">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="1*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<ItemsControl ItemsSource="{Binding MyItems}" d:DataContext="{d:DesignInstance wpfApplication1:ClickableGrid}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="{Binding Columns}"
IsItemsHost="True"
Rows="{Binding Rows}" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate DataType="wpfApplication1:MyItem">
<ToggleButton x:Name="toggleButton" IsChecked="{Binding Path=IsTicked}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<Button Grid.Row="1"
Click="ButtonBase_OnClick"
Content="Check status" />
</Grid>
</Window>
Related
here is what i want: if i bind a ICollectionview to a DataGrid, i dont wanna loose the SortDescription in my Viewmodel.
i create a small sample project to see what i mean. In my projects i simply use a Usercontrol to show my data in a DataGrid. If i do this the SortDescritpion is gone when the UserControl Unload, because the ItemsSource is set to null. If i use a TemplateSelector to show my UserControl, the SortDescription is not gone and the ItemsSource ist not set to null on Unload. the question is, why are these different behaviors? Is one on the 2 behaviors a bug?
btw. I use .Net 4.5.1 but 4.6.1 is installed and system.Windows.Interactivity 4.0.0.0
MainWindow.xaml
<Window x:Class="DataGridICollectionView.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:DataGridICollectionView"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<DataTemplate DataType="{x:Type local:ViewmodelListe}">
<local:MyViewUc/>
</DataTemplate>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<ToolBar Grid.Row="0">
<Button Content="SetWorkspace MyView" Click="Button_Click"/>
<Button Content="SetWorkspace Other" Click="Button_Click_1"/>
</ToolBar>
<ContentPresenter Grid.Row="1" Content="{Binding Workspace}"/>
</Grid>
</Window>
MainWindow.xaml.cs
namespace DataGridICollectionView
{
/// <summary>
/// Interaktionslogik für MainWindow.xaml
/// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged
{
private object _workspace;
public MainWindow()
{
InitializeComponent();
MyViewVm = new ViewmodelListe();
DataContext = this;
}
public ViewmodelListe MyViewVm { get; set; }
public object Workspace
{
get { return _workspace; }
set
{
_workspace = value;
OnPropertyChanged();
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Workspace = MyViewVm;
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
Workspace = "Other";
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
public class ViewmodelListe : INotifyPropertyChanged
{
public ViewmodelListe()
{
Persons = new ObservableCollection<Person>();
MyView = CollectionViewSource.GetDefaultView(Persons);
Persons.Add(new Person() {FirstName = "P1", LastName = "L1"});
Persons.Add(new Person() {FirstName = "P2", LastName = "L2"});
Persons.Add(new Person() {FirstName = "P3", LastName = "L3"});
}
public ObservableCollection<Person> Persons { get; private set; }
public ICollectionView MyView { get; private set; }
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
public class Person : INotifyPropertyChanged
{
private string _firstName;
private string _lastName;
public string FirstName
{
get { return _firstName; }
set
{
_firstName = value;
OnPropertyChanged();
}
}
public string LastName
{
get { return _lastName; }
set
{
_lastName = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
public class TestBehavior : Behavior<DataGrid>
{
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.Unloaded += AssociatedObjectUnloaded;
}
private void AssociatedObjectUnloaded(object sender, RoutedEventArgs e)
{
//look at this in Debug Mode, its NULL if you dont use the TemplateSelector
var itemssource = AssociatedObject.ItemsSource;
}
protected override void OnDetaching()
{
base.OnDetaching();
AssociatedObject.Unloaded -= AssociatedObjectUnloaded;
}
}
}
MyGridControl.xaml
<UserControl x:Class="DataGridICollectionView.MyGridControl"
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:local="clr-namespace:DataGridICollectionView"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<DataGrid ItemsSource="{Binding MyView}" AutoGenerateColumns="True">
<i:Interaction.Behaviors>
<local:TestBehavior/>
</i:Interaction.Behaviors>
</DataGrid>
</Grid>
</UserControl>
MyViewUc.xaml
<UserControl x:Class="DataGridICollectionView.MyViewUc"
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:local="clr-namespace:DataGridICollectionView"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
<DataTemplate x:Key="MyViewCrap">
<local:MyGridControl/>
</DataTemplate>
<local:MyTemplateSelector x:Key="Selector" GridView="{StaticResource MyViewCrap}" />
</UserControl.Resources>
<Grid>
<!--When using Contentcontrol with TemplateSelector- ItemsSource is NOT set to null -->
<ContentControl Content="{Binding .}" ContentTemplateSelector="{StaticResource Selector}"/>
<!--When using MyGridControl withOUT TemplateSelector- ItemsSource is set to NULL -->
<!--<local:MyGridControl/>-->
</Grid>
</UserControl>
MyViewUc.xaml.cs
namespace DataGridICollectionView
{
/// <summary>
/// Interaktionslogik für MyViewUc.xaml
/// </summary>
public partial class MyViewUc : UserControl
{
public MyViewUc()
{
InitializeComponent();
}
}
public class MyTemplateSelector : DataTemplateSelector
{
public DataTemplate GridView { get; set; }
public override DataTemplate SelectTemplate(object item, System.Windows.DependencyObject container)
{
var chooser = item as ViewmodelListe;
if (chooser == null)
{
return base.SelectTemplate(item, container);
}
return GridView;
}
}
}
EDIT: i end up using this
public class MyDataGrid : DataGrid
{
static MyDataGrid ()
{
ItemsSourceProperty.OverrideMetadata(typeof(MyDataGrid ),new FrameworkPropertyMetadata(null, OnPropertyChangedCallBack, OnCoerceItemsSourceProperty));
}
private ICollectionView _defaultView;
protected override void OnItemsSourceChanged(IEnumerable oldValue, IEnumerable newValue)
{
if(_defaultView != null)
_defaultView.CollectionChanged -= LiveSortingPropertiesOnCollectionChanged;
base.OnItemsSourceChanged(oldValue, newValue);
_defaultView = newValue as ICollectionView;
if(_defaultView != null)
_defaultView.CollectionChanged += LiveSortingPropertiesOnCollectionChanged;
}
private void LiveSortingPropertiesOnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Reset)
{
foreach (var dataGridColumn in this.Columns)
{
var isSortDirectionSetFromCollectionView = false;
foreach (var sortDescription in _defaultView.SortDescriptions)
{
if (dataGridColumn.SortMemberPath == sortDescription.PropertyName)
{
dataGridColumn.SortDirection = sortDescription.Direction;
isSortDirectionSetFromCollectionView = true;
break;
}
}
if (!isSortDirectionSetFromCollectionView)
{
dataGridColumn.SortDirection = null;
}
}
}
}
private static void OnPropertyChangedCallBack(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var grd = d as MyDataGrid ;
var view = e.NewValue as ICollectionView;
if (grd == null || view == null)
return;
foreach (var dataGridColumn in grd.Columns)
{
var isSortDirectionSetFromCollectionView = false;
foreach (var sortDescription in view.SortDescriptions)
{
if (dataGridColumn.SortMemberPath == sortDescription.PropertyName)
{
dataGridColumn.SortDirection = sortDescription.Direction;
isSortDirectionSetFromCollectionView = true;
break;
}
}
//wenn die View nicht sortiert war, auch die column nicht Sortieren
if (!isSortDirectionSetFromCollectionView)
{
dataGridColumn.SortDirection = null;
}
}
}
private static object OnCoerceItemsSourceProperty(DependencyObject d, object baseValue)
{
// do nothing here - we just want to override parent behaviour.
// The _only_ thing DataGrid does here is clearing sort descriptors
return baseValue;
}
}
When you host your MyGridControl directly inside MyViewUc (case 1) - when you switch workspace and MyViewUC is unloaded, it's datacontext is set to null. Because MyGridControl is direct child - it's datacontext is set to null too, and in turn DataContext of DataGrid. This sets ItemsSource to null too, because it's bound to DataContext. You can verify this by looking at DataContext of DataGrid in your behavior. This behavior is completely reasonable to my mind.
When you use template selector: MyViewUC is unloaded, it's datacontext is set to null. Then ContentControl Content is set to null, too. Now here is the problem: when you use ContentTemplateSelector, DataContext of your old (unloaded) MyGridControl is NOT set to null. You can verify this in your behaviour, that is why ItemsSource and sort descriptors are preserved.
Now, I believe this second behaviour is not correct and datacontext should be set to null for this unloaded control created by ContentTemplateSelector. The logic behind this is not very simple - you can look yourself at source code of ContentPresenter.OnContentChanged method, where you will see when DataContext is not updated when content changes.
UPDATE: I see your main concern is losing sort descriptors, but that is direct consequence of losing DataContext and setting ItemsSource to null. To me this behaviour looks reasonable, but I see indeed for many people it is not, so that there is even bug report about this issue: https://connect.microsoft.com/VisualStudio/feedback/details/592897/collectionviewsource-sorting-only-the-first-time-it-is-bound-to-a-source
You can see yourself in DataGrid source code that:
protected override void OnItemsSourceChanged(IEnumerable oldValue, IEnumerable newValue)
{
base.OnItemsSourceChanged(oldValue, newValue);
if (newValue == null)
this.ClearSortDescriptionsOnItemsSourceChange();
// more code here....
}
So when you set ItemsSource to null - all sort descriptors are explicitly cleared. At link above you can find some workarounds which you might find useful.
UPDATE2: you can consider trying to fix that behaviour by inheriting from DataGrid. I don't say that is perfect solution, but neither is using ContentTemplateSelector. There are two places where sort descriptors are cleared when ItemsSource is set to null - in OnItemsSourceChanged and OnCoerceItemsSourceProperty. So you can do the following:
public class MyDataGrid : DataGrid {
static MyDataGrid() {
ItemsSourceProperty.OverrideMetadata(typeof(MyDataGrid), new FrameworkPropertyMetadata(null, OnCoerceItemsSourceProperty));
}
private static object OnCoerceItemsSourceProperty(DependencyObject d, object baseValue) {
// do nothing here - we just want to override parent behaviour.
// The _only_ thing DataGrid does here is clearing sort descriptors
return baseValue;
}
protected override void OnItemsSourceChanged(IEnumerable oldValue, IEnumerable newValue) {
SortDescription[] sorts = null;
if (newValue == null) {
// preserve sort descriptors when setting ItemsSource to null
sorts = Items.SortDescriptions.ToArray();
}
// they will now be cleared here
base.OnItemsSourceChanged(oldValue, newValue);
if (sorts != null) {
// restore them back
foreach (var sort in sorts) {
Items.SortDescriptions.Add(sort);
}
}
}
}
With code above you will see that sort descriptors are preserved in your MyView between switching datacontext.
XAML:
<ListBox x:Name="MyTitleBox" ItemsSource="{Binding}"/>
<ListBox x:Name="MyInfoBox" ItemsSource="{Binding}"/>
In C#:
MyTitleBox.ItemsSource = new List<string>(new string[] {"ID:", "Info:", "More Info:"});
MyInfoBox.ItemsSource = new ObservableCollection<string>(MyMainInfo.ToString().Split(',').ToList<string>());
I currently have 2 list boxes next to each other because I need to handle their ItemsSource programmatically.
I know there must be a better way to merge the two. Essentially the list box "on the left" is the titles like ID: and the list box "on the right" is the information.
I thought I could do something like MyTitleBox.Columns.Add like I've seen but it won't let me do .Columns. I'm using .NET 4.
Here is an example with a more MVVM approach:
Domain (The type of items you want to put in your list):
public class Movie : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string _title;
public string Title
{
get { return _title; }
set
{
if (_title != value)
{
_title = value;
RaisePropertyChanged("Title");
}
}
}
private string _info;
public string Info
{
get { return _info; }
set
{
if (_info != value)
{
_info = value;
RaisePropertyChanged("Info");
}
}
}
protected void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
ViewModel:
public class MyViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private ObservableCollection<Movie> _movies;
/// <summary>
/// Collection of movies.
/// </summary>
public ObservableCollection<Movie> Movies
{
get { return _movies; }
set
{
if (_movies != value)
{
_movies = value;
RaisePropertyChanged("Movies");
}
}
}
/// <summary>
/// Constructor
/// </summary>
public MyViewModel()
{
Movies = new ObservableCollection<Movie>();
Movies.Add(new Movie() { Title = "Gravity", Info = "Gravity is about..." });
Movies.Add(new Movie() { Title = "Avatar", Info = "Avatar is about..." });
}
protected void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
XAML:
<Window x:Class="StackOverflow.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:StackOverflow"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<ListBox ItemsSource="{Binding Movies}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock>
<Run Text="{Binding Title}" /><Run Text=" - " /><Run Text="{Binding Info}" />
</TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Grid.Row="1" Content="Click To Change Info" Margin="5" Click="Button_Click" />
</Grid>
</Window>
Code Behind:
public partial class MainWindow : Window
{
public MyViewModel ViewModel { get; private set; }
public MainWindow()
{
InitializeComponent();
ViewModel = new MyViewModel();
DataContext = ViewModel;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Movie movie = ViewModel.Movies.FirstOrDefault();
if (movie != null)
{
movie.Info = "This is the new information";
}
}
}
Implementing INotifyPropertyChanged allows the code to notify the UI when something changes.
If you test this code out you will see that clicking the button updates the info for the first movie, and this changed is immediately reflected in the UI. (Normally you would use a convention like Commands for handling the button click, but for simplicity I did it this way)
Let me know if you have any questions.
Suppose I have a textbox in a WPF Page.
This textbox is bound to a static property which is declared in MainWindowViewModel. Also this static property implements INotifyPropertyChanged.
Here is the code for MainWindowViewModel :
namespace WpfApplication1.ViewModels
{
class MainWindowViewModel : INotifyPropertyChanged
{
public MainWindowViewModel()
{
cPerson = new Person();
SaveChangesCommand = new RelayCommand(SaveChanges);
MainWindowViewModel.StaticPropertyChanged += MainWindowViewModel_StaticPropertyChanged;
}
void MainWindowViewModel_StaticPropertyChanged(object sender, PropertyChangedEventArgs e)
{
}
private static Person _cPerson;
public static Person cPerson
{
get
{
return _cPerson;
}
set
{
_cPerson = value;
OnStaticPropertyChanged("cPerson");
}
}
public ICommand SaveChangesCommand { get; set; }
private void SaveChanges(object obj)
{
//code for saving <-- Gives me values back. i.e. It works fine
using (SampleEntities db = new SampleEntities())
{
db.People.Add(cPerson);
db.SaveChanges();
}
//clear all the fields <-- Here it calls OnStaticPropertyChanged But Fields are not cleared
cPerson = new Person();
}
public static event EventHandler<PropertyChangedEventArgs> StaticPropertyChanged;
protected static void OnStaticPropertyChanged(string PropertyName)
{
EventHandler<PropertyChangedEventArgs> handler = StaticPropertyChanged;
if (handler != null)
{
handler(typeof(MainWindowViewModel), new PropertyChangedEventArgs(PropertyName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string PropertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(PropertyName));
}
}
}
}
Here is the code for Page1ViewModel:
namespace WpfApplication1.ViewModels
{
class Page1ViewModel : INotifyPropertyChanged
{
public Page1ViewModel()
{
}
public Person CurrentPerson
{
get
{
return MainWindowViewModel.cPerson;
}
set
{
MainWindowViewModel.cPerson = value;
OnPropertyChanged("CurrentPerson");
}
}
protected virtual void OnPropertyChanged(string PropertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(PropertyName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
}
I have mentioned my problem in the comments in above code. Still I would like to mention it here:
After I save changes to the database, I want to clear all the textboxes so that user can again fill in the new values.
When I say cPerson = new Person(); Event OnStaticPropertyChanged is raised and all looks fine. When I debug I get cPerson.Name = null and cPerson.Age = null. But when I see the textboxes the old values are not replaced with null.
I have set TargetNullValue = '' in my xaml.
If you like to see the xaml, then here it is:
<Page x:Class="WpfApplication1.Pages.Page1"
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:vm="clr-namespace:WpfApplication1.ViewModels"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
Title="Page1">
<Page.DataContext>
<vm:Page1ViewModel />
</Page.DataContext>
<Canvas DataContext="{Binding DataContext.CurrentPerson, RelativeSource={RelativeSource AncestorType={x:Type Page}}}">
<TextBlock Canvas.Left="10" TextWrapping="Wrap" Text=" Name :" Canvas.Top="44"/>
<TextBox Height="23" Canvas.Left="96" TextWrapping="Wrap"
Text="{Binding Name, UpdateSourceTrigger=LostFocus, TargetNullValue=''}" Canvas.Top="41" Width="120"/>
<TextBlock Canvas.Left="10" TextWrapping="Wrap" Text=" Age :" Canvas.Top="82"/>
<TextBox Height="23" Canvas.Left="96" TextWrapping="Wrap" Text="{Binding Age, UpdateSourceTrigger=LostFocus, TargetNullValue=''}" Width="120" Canvas.Top="80"/>
</Canvas>
</Page>
Try setting target null value as below.
TargetNullValue={x:Static System:String.Empty}
Also add this namespace to your XAML
xmlns:System=”clr-namespace:System;assembly=mscorlib”
I'm trying to create a simple WPF Application using data binding.
The code seems fine, but my view is not updating when I'm updating my property.
Here's my XAML:
<Window x:Class="Calculator.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:calculator="clr-namespace:Calculator"
Title="MainWindow" Height="350" Width="525"
Name="MainWindowName">
<Grid>
<Label Name="MyLabel" Background="LightGray" FontSize="17pt" HorizontalContentAlignment="Right" Margin="10,10,10,0" VerticalAlignment="Top" Height="40"
Content="{Binding Path=CalculatorOutput, UpdateSourceTrigger=PropertyChanged}"/>
</Grid>
</Window>
Here's my code-behind:
namespace Calculator
{
public partial class MainWindow
{
public MainWindow()
{
DataContext = new CalculatorViewModel();
InitializeComponent();
}
}
}
Here's my view-model
namespace Calculator
{
public class CalculatorViewModel : INotifyPropertyChanged
{
private String _calculatorOutput;
private String CalculatorOutput
{
set
{
_calculatorOutput = value;
NotifyPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
var handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
I'm can't see what I am missing here?? o.O
CalculatorOutput has no getter. How should the View get the value? The Property has to be public as well.
public String CalculatorOutput
{
get { return _calculatorOutput; }
set
{
_calculatorOutput = value;
NotifyPropertyChanged();
}
}
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
How can i manage auto switch number in ComboBox in short simple way?
You should not do this in that way. You should present the user swappable user controls which user can drag and drop, changing order with the mouse.
Your solution is not that good, but if you still want to do this, then in combobox selectitem event look through other comboboxes with that value and change it to the previous one.
You can add separate properties in your ViewModel and separate ComboBoxes in the view, then manipulate the values when a value changes in the ViewModel, but this is cumbersome, and involves a lot of work when you add a new ComboBox (value).
A better approach is to create a custom collection that handles the changes:
public class ValueHolder<T> : INotifyPropertyChanged
{
private T _value;
public T Value
{
get { return _value; }
set
{
if (!EqualityComparer<T>.Default.Equals(value, _value))
{
T old = _value;
_value = value;
OnPropertyChanged("Value");
OnValueChanged(old, value);
}
}
}
public ValueHolder()
{
}
public ValueHolder(T value)
{
this._value = value;
}
public event EventHandler<ValueChangedEventArgs<T>> ValueChanged;
public event PropertyChangedEventHandler PropertyChanged;
protected void OnValueChanged(T oldValue, T newValue)
{
var h = ValueChanged;
if (h != null)
h(this, new ValueChangedEventArgs<T>(oldValue, newValue));
}
protected virtual void OnPropertyChanged(string propName)
{
var h = PropertyChanged;
if (h != null)
h(this, new PropertyChangedEventArgs(propName));
}
}
public class ValueChangedEventArgs<T> : EventArgs
{
public T OldValue { get; set; }
public T NewValue { get; set; }
public ValueChangedEventArgs(T oldValue, T newValue)
{
this.OldValue = oldValue;
this.NewValue = newValue;
}
}
public class MyCollection<T> : Collection<ValueHolder<T>>
{
public void Add(T i)
{
this.Add(new ValueHolder<T>(i));
}
private void AddChangeHandler(ValueHolder<T> item)
{
item.ValueChanged += item_ValueChanged;
}
private void RemoveChangeHandler(ValueHolder<T> item)
{
item.ValueChanged -= item_ValueChanged;
}
protected override void InsertItem(int index, ValueHolder<T> item)
{
AddChangeHandler(item);
base.InsertItem(index, item);
}
protected override void RemoveItem(int index)
{
RemoveChangeHandler(this[index]);
base.RemoveItem(index);
}
protected override void ClearItems()
{
foreach (var item in this)
{
RemoveChangeHandler(item);
}
base.ClearItems();
}
protected override void SetItem(int index, ValueHolder<T> item)
{
RemoveChangeHandler(this[index]);
AddChangeHandler(item);
base.SetItem(index, item);
}
private void item_ValueChanged(object sender, ValueChangedEventArgs<T> e)
{
ValueHolder<T> v = (ValueHolder<T>)sender;
for (int i = 0; i < this.Count; i++)
{
if (this[i] == v)
continue;
if (EqualityComparer<T>.Default.Equals(this[i].Value, e.NewValue))
{
this[i].Value = e.OldValue;
break;
}
}
}
}
Then define your ViewModel like this:
public class MyViewModel
{
private MyCollection<int> _values;
public MyViewModel()
{
_values = new MyCollection<int>() { 1, 2, 3, 4, 5 };
}
public MyCollection<int> Values
{
get { return _values; }
}
}
and in your xaml:
<ItemsControl ItemsSource="{Binding Path=Values}" Margin="10">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<ComboBox SelectedValue="{Binding Path=Value}" Margin="5">
<ComboBox.ItemsSource>
<Int32Collection >1,2,3,4,5</Int32Collection>
</ComboBox.ItemsSource>
</ComboBox>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
This is a lot of code! But does the task nicely, and also is very maintainable.
Just switch values in your data model. As a result wpf controls will be updated when properties of the data model are changed.
XAML:
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:model="clr-namespace:WpfApplication2"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<model:DataModel x:Key="MyModel" />
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<ItemsControl Grid.Column="0" Grid.Row="0" DataContext="{StaticResource MyModel}"
ItemsSource="{Binding Items}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<ComboBox SelectedItem="{Binding SelectedItem}" ItemsSource="{Binding Items}">
</ComboBox>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
</Window>
Code:
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
namespace WpfApplication2
{
public class DataModel
{
#region Construction and Initialization
public DataModel()
{
var elements = new[] {1, 2, 3, 4, 5};
Items = new List<ItemsModel>
{
new ItemsModel(elements, 1),
new ItemsModel(elements, 2),
new ItemsModel(elements, 3),
new ItemsModel(elements, 4),
new ItemsModel(elements, 5)
};
foreach (var itemsModel in Items)
{
itemsModel.PropertyChanged += SelectedItemChanged;
}
}
#endregion
public List<ItemsModel> Items { get; private set; }
private void SelectedItemChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "SelectedItem")
{
var model = sender as ItemsModel;
Debug.Assert(model != null, "model != null");
int pos = Items.IndexOf(model) + 1;
Items[model.SelectedItem - 1].SelectedItem = pos;
}
}
}
public class ItemsModel: INotifyPropertyChanged
{
#region Construction and Initialization
public ItemsModel(IEnumerable<int> items, int selectedItem)
{
Items = items;
_selectedItem = selectedItem;
}
#endregion
public int SelectedItem
{
get { return _selectedItem; }
set
{
if (_selectedItem != value)
{
_selectedItem = value;
RaisePropertyChanged("SelectedItem");
}
}
}
private int _selectedItem;
public IEnumerable<int> Items { get; private set; }
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}