Referencing datepicker seleteddate from inside datagrid - wpf

New to programming and need a little assistance. I have a WPF usercontrol used for an Outlook Add-in and everything was going swimmingly until I tried a datagrid with a datepicker in the celleditingtemplate column but the celltemplatecolumn simply has a textblock. The idea is to have the textblock bound to the datasource (which is working fine) and when the user edits the column, the datepicker shows and the user can select a date. I simply cannot figure out how to send the value from the selecteddate to the textblock and I'm completely at my wits end. Below is the datagrid with its bindings, simple SQL table with the following fields, [Project],[Deadline],[DaystoDeadline],[Responibility],[ProcessCount],[TasksCompleted],[TasksRemaining]. Any assistance would be greatly appreciated.
<Grid>
<DataGrid AutoGenerateColumns="False" HorizontalAlignment="Left" Margin="6,0,0,0" Name="ProjectsGrid" ScrollViewer.CanContentScroll="True" VerticalAlignment="Top" VerticalScrollBarVisibility="Auto" ItemsSource="{Binding}" CanUserAddRows="False" CanUserDeleteRows="False">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Commands">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Click="Select_Button" Content="Select" Name="SelectButton" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Binding="{Binding Project}" Header="Project" IsReadOnly="True" FontFamily="Verdana" />
<DataGridTemplateColumn Header="Deadline">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Deadline, StringFormat='yyyy/MM/dd'}" FontFamily="Verdana" x:Name="DeadlineTxt" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<DatePicker SelectedDate="{Binding Deadline, StringFormat='yyyy/MM/dd'}" FontFamily="Verdana" x:Name="DeadlineDatePicker">
<DatePicker.CalendarStyle>
<Style TargetType="Calendar">
<Setter Property="DisplayMode" Value="Month"/>
</Style>
</DatePicker.CalendarStyle>
</DatePicker>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Binding="{Binding DaystoDeadline}" Header="Days to Deadline" IsReadOnly="True" FontFamily="Verdana" />
<DataGridTextColumn Binding="{Binding Responsibility}" Header="Responsibility" IsReadOnly="True" FontFamily="Verdana" />
<DataGridTextColumn Binding="{Binding Processes}" Header="Processes" IsReadOnly="True" FontFamily="Verdana" />
<DataGridTextColumn Binding="{Binding Completed}" Header="Completed" IsReadOnly="True" FontFamily="Verdana" />
<DataGridTextColumn Binding="{Binding Remaining}" Header="Remaining" IsReadOnly="True" FontFamily="Verdana" />
</DataGrid.Columns>
</DataGrid>

I would start by taking off the names of the controls in the DataTemplate (x:Name="DeadlineTxt" x:Name="DeadlineDatePicker") and remove the StringFormat from the SelectedDate: (SelectedDate="{Binding Deadline}")
<DataGridTemplateColumn Header="Deadline">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Deadline, StringFormat='yyyy/MM/dd'}" FontFamily="Verdana" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<DatePicker SelectedDate="{Binding Deadline}" FontFamily="Verdana" >
<DatePicker.CalendarStyle>
<Style TargetType="Calendar">
<Setter Property="DisplayMode" Value="Month"/>
</Style>
</DatePicker.CalendarStyle>
</DatePicker>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>

Related

Why datepicker not appearing in my datagrid

Why datepicker is not appearing in my datagrid.
I have textblock, one datepicker and then again textblock in total three columns, Those two textblocks appears properly but the datepicker do not appear. whereas there is no problem in binding because if a have applied same in the listview and there they are appearing, it's something wrong i have done in xaml in datagrid which i am not able to figure it out. I want user to select dates from this datepicker.
Below is my code, what i am missing any idea ?
<DataGrid AutoGenerateColumns="False"
ColumnWidth="*"
AutoGenerateColumns="False"
CanUserSortColumns="False"
ItemsSource="{Binding EquipMaintCollection}"
SelectedIndex="{Binding SelectedIndexOfItem}"
SelectedItem="{Binding SelectedMaintElement}">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Name}" Header="{x:Static res:LocalizedString.Name}" />
<DataGridTemplateColumn Header="MainteDue">
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<DatePicker x:Name="MainteDue" SelectedDate="{Binding MainteDue, Mode=TwoWay}" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=MainteDue,Mode=TwoWay,StringFormat={}{0:dd/MM/yyyy}}" VerticalAlignment="Center" HorizontalAlignment="Left">
</TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="Until Due" Width="*" MinWidth="100" Foreground="{Binding UntilDue, Converter={StaticResource BoolToRedBrushConverter}}" Binding="{Binding Path=UntilDue}" />
</DataGrid.Columns>
</DataGrid>

WPF DataGrid TemplateColumn not switching between edit and normal templates

I have a WPF DataGrid and I am trying to display a TextBlock when not in edit mode and a ComboBox when editing. I would think this would be easy, but it is not. I can only get it to working using a brute force method of placing the controls in a Grid and using a Visibility binding to show the controls in turn; which is a real pain.
Why does the following not work:
I can get one or the other to show, but not both based on cell editing.
<DataGrid Background="DarkGray" ItemsSource="{Binding Items}" CanUserAddRows="false" AutoGenerateColumns="False"
ScrollViewer.CanContentScroll="True" ScrollViewer.HorizontalScrollBarVisibility="Auto"
ScrollViewer.VerticalScrollBarVisibility="Auto"
SelectionChanged="DataGrid_SelectionChanged" SelectedIndex="{Binding SelectedIndex}"
behaviors:DataGridBehavior.OnSelectAction="{Binding Path=OnSelectionChanged}">
<DataGrid.Columns>
<DataGridTextColumn Header="Button" Binding="{Binding Button}" />
<DataGridTextColumn Header="Button Display" Binding="{Binding ButtonDisplay}" />
<DataGridTextColumn Header="Reason Code" Binding="{Binding ReasonCode}" />
<!--<DataGridTextColumn Header="Count Stamps" Binding="{Binding CountStamps}" />-->
<DataGridTemplateColumn Header="Count Stamps">
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox IsEditable="True" SelectedValue="{Binding CountStamps}"
ItemsSource="{Binding Values}" DisplayMemberPath="Name" SelectedValuePath="Value" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding CountStamps}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="Description" Binding="{Binding Description}" Width="*" />
</DataGrid.Columns>

How to work on control inside Datagrid in WPF?

I m stuck in minor issue,which become time consuming as per my project concern,i am using datagrid in my wpf project in which my requirement is to show payment button only when status is "Not Paid",here i had use DataGridTemplateColumn in which i m using Button and on grid only i m getting status of payment.
Here is my xaml File
<DataGrid Name="gridInvoiceList" SelectedItem="{Binding SelectedDetails}" ColumnHeaderHeight="35" ItemsSource="{Binding ItemsSource, ElementName=pageControl, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" CanUserAddRows="false"
HorizontalAlignment="Left" AutoGenerateColumns="False"
Width="978" RowHeaderWidth="20" RowHeight="25"
CanUserSortColumns="False" SelectionChanged="gridInvoiceList_SelectionChanged" Height="220" LoadingRow="gridInvoiceList_LoadingRow" SelectionMode="Extended" >
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Width="160" Binding="{Binding FullName }" CanUserSort="True" IsReadOnly="True" />
<DataGridTextColumn Header="Total Amount" Width="150" Binding="{Binding TotalAmount,ConverterCulture='en-GB',StringFormat=-{0:C}}" CanUserSort="True" IsReadOnly="True" />
<DataGridTextColumn Header="Invoice Date" Width="200" Binding="{Binding InvoiceDate}" CanUserSort="True" IsReadOnly="True"/>
<DataGridTextColumn Header="Payment Status" Width="220" Binding="{Binding PaymentStatus}" CanUserSort="True" IsReadOnly="True" />
<DataGridTemplateColumn Header="Pay" Width="105">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="Pay" HorizontalAlignment="Center" VerticalAlignment="Top" Name="btnPay" Click="btnPay_Click" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Print" Width="105">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="Print" HorizontalAlignment="Center" VerticalAlignment="Top" Name="btnPrintInvoice" Click="btnPrint_Click" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
I had use some code from reference code which had not fullfill my requirement
You can use a converter to achieve that. But I'll you show you the simplest, easiest way:
Create a property:
public System.Windows.Visibility PayButtonVisibility
{
get
{
if (PaymentStatus == MyEnum.NotPaid)
return System.Windows.Visibility.Visible;
return System.Windows.Visibility.Collapsed;
}
}
Then bind your button visibility to it:
<Button Content="Pay" HorizontalAlignment="Center" VerticalAlignment="Top" Name="btnPay" Click="btnPay_Click" Visibility="{Binding PayButtonVisibility}" />

Nesting Wrappanel into DataGrid

I am trying to nest a WrapPanel inside a DataGrid cell. What am I missing?
This is used to display sports team information.
A Team has a TeamName, a Coach, a Roster of Players where each player has a FullName.
More technically speaking, the Roster property is a ObservableCollection where PlayerViewModel has FullName property.
<DataGrid ItemsSource="{Binding Teams}">
<DataGrid.Columns>
<DataGridTextColumn Header="Team Name" Binding="{Binding TeamName}" />
<DataGridTextColumn Header="Coach" Binding="{Binding Coach}" />
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<WrapPanel DataContext="{Binding Roster}">
<Label Content="{Binding FullName}">
</WrapPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
I figured it out. This worked.
<DataGridTemplateColumn Width="150">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<WrapPanel DataContext="{Binding Roster}">
<Label HorizontalAlignment="Center" Content="{Binding Path=FirstName}" />
</WrapPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

WPF databinding a gridview issue

I am working on a dataGrid that is populated from an Entity model. I have the following for the specific column as an attempt to use a combobox during editing and the source for the editing is a CollectionsViewSource.
<Grid DataContext="{StaticResource vsLogins}" Grid.Column="1"
Margin="16,248,154,31">
<DataGrid AutoGenerateColumns="False" EnableRowVirtualization="True"
Height="213" HorizontalAlignment="Left"
ItemsSource="{Binding Source={StaticResource vsLogins}}"
Name="LoginsDataGrid"
RowDetailsVisibilityMode="VisibleWhenSelected"
VerticalAlignment="Top" Width="380"
Background="{StaticResource lgb}" BorderThickness="2"
BorderBrush="#FFFC0303">
<DataGrid.Columns>
<DataGridTextColumn x:Name="LoginNameColumn" Binding="{Binding
Path=LoginName}" Header="Login Name"
Width="200" MinWidth="200" />
<DataGridTextColumn x:Name="PsWrdColumn"
Binding="{Binding Path=PsWrd}"
Header="Password" Width="130" MinWidth="130" />
<DataGridTemplateColumn x:Name="AccessLevelIdColumn" Header="Id"
Width="40" MinWidth="40">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=AccessLevelId}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Source={StaticResource
vsAccessLevels}}" DisplayMemberPath="Description"
SelectedValuePath="AccessLevelId" IsEditable="False"/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
It is throwing some errors. What am I doing wrong? I also tried putting a grid around the combobox and setting it's dataContent to the CVS - no dice either. Thanks for any help.
<DataGridComboBoxColumn x:Name="AccessIdColumn"
ItemsSource="{Binding Source={StaticResource vsAccessLevels}}"
DisplayMemberPath="Description"
SelectedValuePath="AccessLevelId"
SelectedValueBinding="{Binding Path=AccessLevelId}"/>

Resources