Multi selection in WPF Listview and multiple styles - c#

I have an application in WPF (MVVM).
I've created a view model which points to an ObservableCollections to be shown in the ListView.
The ListView has two custom view resources. 'GridView' and a 'TileView' and buttons to switch between them.
When I select multiple items and change to the other view the selected items are not sync...
after trying to debug I think that, for some reason, when changing views it:
Gets the IsSelected value for every Item in the list (That's OK)
Sets (again..) the Items that have just been set (in the other view..) (?)
The markup:
<Window.Resources>
<local:TileView x:Key="ImageView">
<local:TileView.ItemTemplate >
<DataTemplate >
<StackPanel Width="150" VerticalAlignment="Top">
<Image Source="{Binding Path=ImagePath}"/>
<TextBlock TextWrapping="Wrap" HorizontalAlignment="Center" Text="{Binding Path=PhotoReferenceCode}"/>
</StackPanel>
</DataTemplate>
</local:TileView.ItemTemplate>
</local:TileView>
<GridView x:Key ="ListView">
<GridViewColumn Header="Select" Width="100" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<Grid Width="100">
<!--<CheckBox IsChecked="{Binding Path=IsSelected, Mode=TwoWay}" HorizontalAlignment="Center"/>-->
</Grid>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Image" Width="100" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<Image Source="{Binding Path=ImagePath}" Stretch="UniformToFill" HorizontalAlignment="Center" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Reference Code" Width="100" DisplayMemberBinding="{Binding Path=PhotoReferenceCode}"/>
</GridView>
</Window.Resources>
<Grid>
<ListView Margin="0,36,0,0" View="{Binding Path=ViewType, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}" SelectionMode="Multiple"
ItemsSource="{Binding Path=InfoList, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}">
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="IsSelected" Value="{Binding Path=IsSelected, Mode=TwoWay}"/>
</Style>
</ListView.ItemContainerStyle>
</ListView>
<Button Name="btnListView" Content="ListView" Click="btnListView_Click" HorizontalAlignment="Left" Margin="10,9,0,0" VerticalAlignment="Top" Width="75"/>
<Button Name="btnImageView" Content="ImageView" Click="btnImageView_Click" HorizontalAlignment="Left" Margin="99,9,0,0" VerticalAlignment="Top" Width="75"/>
</Grid>
The Item class:
public class ItemData : INotifyPropertyChanged
{
#region Members
private bool _IsSelected;
public string ImagePath { get; set; }
public string PhotoReferenceCode { get; set; }
#endregion
#region Constractors
public ItemData()
{
}
public ItemData(bool Is_Sected, string Image_Path, int Item_Carat, string Photo_Reference_Code)
{
IsSelected = Is_Sected;
ImagePath = Image_Path;
ItemCarat = Item_Carat;
PhotoReferenceCode = Photo_Reference_Code;
}
#endregion
#region Public
public bool IsSelected
{
get { return _IsSelected; }
set
{
if (this.PropertyChanged != null && _IsSelected != value)
{
_IsSelected = value; OnPropertyChanged("IsSelected");
}
}
}
#endregion
#region Events
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
Any help would be appreciated.

Bind all of the listview's SelectedItems to a property in the view model using TwoWay bind. So the viewModel keeps the selectedItems of the listview and so when the view is changed, the new listview style can pick up the selectedItems from the viewModel

Related

How to toggle visibility of a single DataGridRow in WPF?

Goal
I am aiming to create a button that triggers selected row RowDetailsTemplate visibility.
Problem
I somewhat managed to do it, but on my button click.. it displays RowDetailsTemplates for every single record. I need it to display the selected row, not all.
Collapsed
Visible
Question
How can I only trigger the selected row visibility state?
Code
XAML
<DataGrid ItemsSource="..." SelectedItem="..." IsReadOnly="True">
<DataGrid.Columns>
<DataGridTextColumn Header="Product Code" Binding="{Binding ProductCode}" />
<DataGridTemplateColumn Header="Actions">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Button Content="Edit" Command="{Binding RelativeSource={RelativeSource AncestorType=DataGrid}, Path=DataContext.TriggerVisibility }" />
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<StackPanel Background="Orange">
<TextBlock Text="Test" />
</StackPanel>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
<DataGrid.RowStyle>
<Style TargetType="{x:Type DataGridRow}">
<Setter Property="DetailsVisibility" Value="{Binding RelativeSource={RelativeSource AncestorType=DataGrid}, Path=DataContext.IsVisible}" />
</Style>
</DataGrid.RowStyle>
</DataGrid>
View Model
public ICommand TriggerVisibility { get; }
private void GetVisibleCondition()
{
if(IsVisible == Visibility.Visible)
{
IsVisible = Visibility.Collapsed;
}
else if(IsVisible == Visibility.Collapsed)
{
IsVisible = Visibility.Visible;
}
}
private Visibility _isVisible = Visibility.Collapsed;
public Visibility IsVisible
{
get { return _isVisible; }
set
{
_isVisible = value;
OnPropertyChanged(nameof(IsVisible));
}
}
You cannot use a single property of a single view model to toggle the visibility of each individual row.
You should add an IsDetailsVisibile property at row level, i.e. to your data object T in the IEnumerable<T> ItemsSource of the DataGrid:
public class Product : INotifyPropertyChanged
{
public Product()
{
TriggerVisibility = new RelayCommand2(() => IsDetailsVisibile = !IsDetailsVisibile);
}
public string ProductCode { get; set; }
public ICommand TriggerVisibility { get; }
private bool _isDetailsVisibile;
public bool IsDetailsVisibile
{
get { return _isDetailsVisibile; }
set { _isDetailsVisibile = value; NotifyPropertyChanged(); }
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "") =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public class RelayCommand : ICommand
{
private readonly Action _execute;
public RelayCommand(Action execute) => _execute = execute;
public event EventHandler CanExecuteChanged;
public bool CanExecute(Object parameter) => true;
public void Execute(Object parameter) => _execute();
}
XAML:
<DataGrid.Columns>
<DataGridTextColumn Header="Product Code" Binding="{Binding ProductCode}" />
<DataGridTemplateColumn Header="Actions">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Button Content="Edit" Command="{Binding TriggerVisibility}" />
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<StackPanel Background="Orange">
<TextBlock Text="Test" />
</StackPanel>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
<DataGrid.RowStyle>
<Style TargetType="{x:Type DataGridRow}">
<Style.Resources>
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
</Style.Resources>
<Setter Property="DetailsVisibility" Value="{Binding IsDetailsVisibile,
Converter={StaticResource BooleanToVisibilityConverter}}" />
</Style>
</DataGrid.RowStyle>

How to execute command on button in WPF?

I need to have a button in each ListViewItem. I've created the Button in DataTemplate, bound the command and it doesn't get executed when I press the button. It just doesn't being called.
I was referring to different tutorials and questions like
WPF Button doesn't execute Command or How to bind WPF button to a command in ViewModelBase? and created a RelayCommand class, which implements ICommand.
Actually, I need to call the action with the parameter, but I can't even get it to work without parameters, so I'm planning to get to it next. Everything else is bound perfectly and works like a charm.
View
<Page.Resources>
<CollectionViewSource x:Key='src'
Source="{Binding TimesheetEntries}"
>
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="Date" />
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
</Page.Resources>
<Page.DataContext>
<ViewModels:TimesheetViewModel/>
</Page.DataContext>
<ListView
x:Name="TimesheetEntriesListView"
Margin="10"
Grid.Row="1"
Grid.ColumnSpan="2"
ItemsSource="{Binding Source={StaticResource src}}"
SelectedItem="{Binding SelectedEntry, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
>
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Height="30" Margin="3" IsEnabled="{Binding IsEditable}">
<ComboBox
SelectedValuePath="Key" DisplayMemberPath="Value"
ItemsSource="{Binding EmploymentTypesDictionary, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
SelectedValue="{Binding SelectedEmployment, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Width="300"/>
<TextBox
Text="{Binding Hours, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, StringFormat=N2}"
Margin="5,0,0,0"
Height="Auto"
IsEnabled="{Binding HoursAvaliable}"
Width="70"/>
<Button Margin="5,0,10,0"
Content="+"
Command="{Binding AddNewTimesheetEntryCommand}"
CommandParameter="{Binding Path=Name}"
></Button>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<StackPanel Margin="5,5,5,0" Orientation="Horizontal">
<TextBlock FontSize="14" Text="{Binding Path=Name, StringFormat='{}{0:dd/MM/yyyy, dddd}'}"/>
</StackPanel>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</ListView.GroupStyle>
</ListView>
ViewModel
class TimesheetViewModel : BaseViewModel
{
public ICommand AddNewTimesheetEntryCommand
{
get
{
return _AddNewTimesheetEntryCommand ?? new RelayCommand(AddNewTimesheetEntry);
}
}
private ICommand _AddNewTimesheetEntryCommand;
public void AddNewTimesheetEntry(object parameter)
{
//Do stuff
}
public TimesheetViewModel()
{
}
}
RelayCommand
public class RelayCommand : ICommand
{
private Action<object> mAction;
public event EventHandler CanExecuteChanged = (sender, e) => { };
public RelayCommand(Action<object> action)
{
mAction = action;
}
public bool CanExecute(object parameter)
{
return true;
}
public void Execute(object parameter)
{
mAction(parameter);
}
}
Your button need to have been different bind, beacuse inside the list-template you do not have access to global DataContext only to local. You need to use relative source to access global DataContext.
Command="{Binding Path=DataContext.AddNewTimesheetEntryCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Page}}}"

WPF - how to do binding the right way for a particular scenario?

I'm pretty new to WPF (moving from WinForms). I'm trying to transfer some scenario from a WinForms application to a WPF one:
A window has a ListView control with 3 columns.
There is a button there to add new rows to that ListView.
The first and the second columns contain the ComboBox control.
The third column must contain different controls but just one at a time is visible. Which one is visible, it depends on the selected value of the ComboBox at the first column.
The content of the ComboBox at the second column changes every time a user selects a value from the ComboBox at the first column.
The general scenario is: a user selects a type from the list of types from the first ComboBox, after that the second ComboBox changes its content to a list of supported operations for the selected type and the third column at that time must change its content to display a control that supports the input for that type.
I know how to implement it using WinForms but I have no idea yet how to do it using WPF. Can someone help me to implement it or can anyone help with the information that facilitate implementing that?
I have the code so far:
public class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
}
protected virtual void OnPropertyChanged(PropertyChangedEventArgs args)
{
if (PropertyChanged != null) PropertyChanged(this, args);
}
}
public class RecordFilter : ViewModelBase
{
private static readonly ObservableCollection<KeyValuePair<PropertyInfo, string>> ColumnAliases =
new ObservableCollection<KeyValuePair<PropertyInfo, string>>(Card.ColumnAliases);
private KeyValuePair<PropertyInfo, string> _currentSelectedProperty;
public IEnumerable<OperationInfo> Operations
{
get
{
return Operations.GetOperationInfosForType(GetTypeUnwrapNullable(SelectedProperty.Key.PropertyType));
}
}
public OperationInfo SelectedOperation { get; set; }
public KeyValuePair<PropertyInfo, string> SelectedProperty
{
get { return _currentSelectedProperty; }
set
{
_currentSelectedProperty = value;
OnPropertyChanged("Operations");
}
}
public ObservableCollection<KeyValuePair<PropertyInfo, string>> Properties
{
get { return ColumnAliases; }
}
//DateTime or int or float, depends on the selected property type
//public object PropertyValue { get; set; }
}
Here is the XAML code:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Converters="clr-namespace:App.Converters" x:Class="App.DialogWindows.CardFilterWindow"
Title="Search filters" Height="347" Width="628" x:Name="wdw" ShowInTaskbar="False" WindowStartupLocation="CenterScreen">
<Window.Resources>
<Converters:NotNullObjectToEnabledConverter x:Key="NotNullObjectToEnabledConverter"/>
</Window.Resources>
<DockPanel>
<StackPanel DockPanel.Dock="Bottom" Orientation="Horizontal" HorizontalAlignment="Center" Height="Auto">
<Button x:Name="bnOK" Margin="5" Width="41" Content="OK" IsDefault="True" Click="bnOK_Click"/>
<Button x:Name="bnCancel" Margin="5" Content="Отмена" IsCancel="True"/>
</StackPanel>
<ListView ItemsSource="{Binding Filters, ElementName=wdw}" Name="LvExpr" DataContext="{Binding Filters, ElementName=wdw}">
<ListView.Resources>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListView.Resources>
<ListView.View>
<GridView>
<GridViewColumn Header="Alias" Width="210">
<GridViewColumn.CellTemplate>
<DataTemplate>
<ComboBox VerticalAlignment="Center"
ItemsSource="{Binding Properties}"
DisplayMemberPath="Value"
SelectedValue="{Binding SelectedProperty, Mode=TwoWay}"
/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Operation" Width="150">
<GridViewColumn.CellTemplate>
<DataTemplate>
<ComboBox VerticalAlignment="Center"
ItemsSource="{Binding Operations}"
DisplayMemberPath="OperationAlias"
SelectedValue="{Binding SelectedOperation, Mode=TwoWay}"
/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Value" Width="100">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Text="ValidatesOnDataErrors=True}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="33">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button Tag="{Binding Mode=OneWay}" Click="BnDelete_Click" ToolTip="Delete filter">
<Image Source="delete.ico" Height="16" Width="16"/>
</Button>
</DataTemplate>
</GridViewColumn.CellTemplate>
<GridViewColumnHeader>
<DataGridCell>
<Button Click="ButtonAdd_Click" Height="22" Padding="0" ToolTip="Add filter">
<Image Source="plus.ico" Focusable="False"/>
</Button>
</DataGridCell>
</GridViewColumnHeader>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</DockPanel>
</Window>
In your view-model, set up the list properties, and filter them out accordingly when the selected value changes (via the INotifyPropertyChanged.PropertyChanged event).
See this post for a comprehensive example. It uses a technique called MVVM that is used extensively with WPF and stands for ModelViewViewModel. I highly recommend you to learn this technique and utilize it in your XAML-related projects.
Here is one quick start tutorial, out of the many on the net.

WPF-MVVM Radio Button generated automatically

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.

DataTemplate Binding Error

I am having a problem trouble shooting a binding error in a datatemplate. I create an observable collection, and set mySelectFlag True for each filename. When the Datagrid displays, the checkbox on the header is checked, and each row in the datagrid has its checkbox checked. If I select/unselect a row checkbox, the change shows up on the observable collection. If I uncheck the header checkbox, I set each record on the observable collection to false. I call OnPropertyChanged on the observable collection, but the datagrid does not reflect the change. There are no binding errors. Can anyone tell me what I am doing wrong?
Thank you very much.
I have a class:
public class InputFileName
{
public bool mySelectFlag { get; set; }
public string myFileName { get; set; }
}
and a ObservableCollection:
private ObservableCollection<InputFileName> _DisplayList;
public ObservableCollection<InputFileName> DisplayList
{
get { return _DisplayList; }
set
{
if (value != _DisplayList)
{
_DisplayList = value;
OnPropertyChanged("DisplayList");
}
}
}
and this is my xaml:
<DataGrid
Margin="25"
RowHeaderWidth="0"
AutoGenerateColumns="False"
AlternatingRowBackground="Gainsboro"
AlternationCount="2"
Block.TextAlignment="Center"
CanUserAddRows="False"
CanUserReorderColumns="False"
CanUserResizeColumns="False"
CanUserSortColumns="False"
ItemsSource="{Binding Path=DisplayList}" >
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.HeaderStyle>
<Style TargetType="DataGridColumnHeader">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<CheckBox
IsChecked="{Binding Path=DataContext.IsSelected, ElementName=myControl1, FallbackValue=False}"
Command="{Binding Path=DataContext.SelectAllRows, ElementName=myControl1}"
Content="Select"
FontWeight="Bold"
Width="Auto" />
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</DataGridTemplateColumn.HeaderStyle>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox
HorizontalAlignment="Center"
VerticalAlignment="Center"
IsChecked="{Binding Path=mySelectFlag, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, FallbackValue=False}"
Command="{Binding Path=DataContext.TestTaskCommand, ElementName=myControl1}"
CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=SelectedIndex}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn
Header="File Name"
FontWeight="Bold"
Width="Auto"
Binding="{Binding Path=myFileName}" />
</DataGrid.Columns>
</DataGrid>
You need to implement INotifyPropertyChanged interface on your InputFileName class as weill to make changes visible to UI -
private bool mySelectFlag;
public bool MySelectFlag
{
get
{
return mySelectFlag;
}
set
{
mySelectFlag = value;
OnPropertyChanged("MySelectFlag");
}
}
INotifyPropertyChanged interfaceto be implemented by your class, a method should be defined like the one below and all the properties should call the OnPropertyChangeEvent
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string e)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(e));
}

Categories