[英]WPF C# Datagrid Not Adding Row Unless I have another control to get focus
I have a empty datagrid bound to an observable collection of custom objects. 我有一个空的datagrid绑定到可观察的自定义对象集合。 When I try to create a new row through the UI I get a new row but it never seems to commit the new row unless I have another control for the UI to give focus to.
当我尝试通过UI创建新行时,会得到一个新行,但是除非我有另一个可以使UI焦点集中的控件,否则它似乎永远不会提交新行。 Without the other control I can just tab through each column of the new row and it is an endless circle.
如果没有其他控件,我只能在新行的每一列之间切换,这是一个无休止的圆圈。 If I change the Textblock to a textbox then I can tab to the textbox and I get the new row.
如果将“文本块”更改为文本框,则可以跳至该文本框,然后得到新行。
Below is all of my code and XAML. 以下是我的所有代码和XAML。
XAML: XAML:
<Window x:Class="WPFDatagridTesting.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<Window.Resources>
<CollectionViewSource x:Name="MultiplierCollection" x:Key="MultiplierCollection"></CollectionViewSource>
</Window.Resources>
<Grid>
<DockPanel LastChildFill="True">
<TextBlock Name="NumberTotal" DockPanel.Dock="Bottom">test</TextBlock>
<DataGrid Name="TestGrid"
CanUserAddRows="True"
AutoGenerateColumns="False"
CanUserDeleteRows="True"
CanUserReorderColumns="False"
ItemsSource="{Binding Source={StaticResource MultiplierCollection}}" IsSynchronizedWithCurrentItem="True" AlternatingRowBackground="#FFF7EF9E" AlternationCount="1" SelectionUnit="CellOrRowHeader" ClipboardCopyMode="IncludeHeader">
<DataGrid.Resources>
<Style x:Key="errorStyle" TargetType="{x:Type TextBox}">
<Setter Property="Padding" Value="-2"/>
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="True">
<Setter Property="Background" Value="Red"/>
<Setter Property="ToolTip"
Value="{Binding RelativeSource={RelativeSource Self},
Path=(Validation.Errors)[0].ErrorContent}"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn EditingElementStyle="{StaticResource errorStyle}" Binding="{Binding BaseNumber,ValidatesOnDataErrors=True,ValidatesOnExceptions=True, Mode=TwoWay,NotifyOnSourceUpdated=true, UpdateSourceTrigger=LostFocus}" Header="Base Number"></DataGridTextColumn>
<DataGridTextColumn EditingElementStyle="{StaticResource errorStyle}" Binding="{Binding Multiplier,ValidatesOnDataErrors=True,ValidatesOnExceptions=True, Mode=TwoWay,NotifyOnSourceUpdated=true,UpdateSourceTrigger=LostFocus}" Header="Multiplier"></DataGridTextColumn>
<DataGridTextColumn EditingElementStyle="{StaticResource errorStyle}" Binding="{Binding Result,ValidatesOnDataErrors=True,ValidatesOnExceptions=True, Mode=OneWay,NotifyOnSourceUpdated=true,UpdateSourceTrigger=LostFocus}" Header="Result"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
</DockPanel>
</Grid>
</Window>
Code-Behind: 代码隐藏:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WPFDatagridTesting.CustomObjects;
namespace WPFDatagridTesting
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
MultiplierObservableCollection mycollection = new MultiplierObservableCollection();
System.Windows.Data.CollectionViewSource MultiplierCollection;
private void Window_Loaded(object sender, RoutedEventArgs e)
{
try
{
mycollection = new MultiplierObservableCollection();
mycollection.TotalResultsChanged +=mycollection_TotalResultsChanged;
MultiplierCollection = ((System.Windows.Data.CollectionViewSource)(this.FindResource("MultiplierCollection")));
MultiplierCollection.Source = mycollection.MultiplierClassList;
}
catch (Exception)
{
throw;
}
}
void mycollection_TotalResultsChanged(object sender, MultiplierCollectionResultsChangedEventArgs e)
{
this.NumberTotal.Text = e.MultiplierResults.ToString();
}
}
}
Custom Objects: 自定义对象:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
namespace WPFDatagridTesting.CustomObjects
{
class MultiplierClass : INotifyPropertyChanged
{
protected virtual void Changed(string PropertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(PropertyName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
public MultiplierClass()
{
}
private int _BaseNumber;
public int BaseNumber
{
get
{
return _BaseNumber;
}
set
{
if (_BaseNumber != value)
{
_BaseNumber = value;
Changed("BaseNumber");
Changed("Result");
}
}
}
private int _Multiplier;
public int Multiplier
{
get
{
return _Multiplier;
}
set
{
if (_Multiplier != value)
{
_Multiplier = value;
Changed("Multiplier");
Changed("Result");
}
}
}
public int Result
{
get
{
return _BaseNumber * _Multiplier;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
namespace WPFDatagridTesting.CustomObjects
{
class MultiplierObservableCollection : ObservableCollection<MultiplierClass>
{
public ObservableCollection<MultiplierClass> MultiplierClassList { get; set; }
public MultiplierObservableCollection() : base()
{
MultiplierClassList = new ObservableCollection<MultiplierClass>();
MultiplierClassList.CollectionChanged += MultiplierClassList_CollectionChanged;
}
void MultiplierClassList_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
{
foreach (MultiplierClass item in e.NewItems)
{
//Added items
item.PropertyChanged += myNewClass_PropertyChanged;
}
}
if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove)
{
MultiplierClass myNewClass = new MultiplierClass();
myNewClass.PropertyChanged -= myNewClass_PropertyChanged;
}
myNewClass_PropertyChanged(this, null);
}
void myNewClass_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
MultiplierCollectionResultsChangedEventArgs myEventArgs = new MultiplierCollectionResultsChangedEventArgs();
myEventArgs.MultiplierResults = this.MultiplierClassList.Sum(mc => mc.Result);
EventHandler<MultiplierCollectionResultsChangedEventArgs> handler = TotalResultsChanged;
if (handler != null)
{
handler(this, myEventArgs);
}
}
public event EventHandler<MultiplierCollectionResultsChangedEventArgs> TotalResultsChanged;
}
public class MultiplierCollectionResultsChangedEventArgs : EventArgs
{
public int MultiplierResults { get; set; }
}
}
You might need to change 您可能需要更改
ItemsSource="{Binding Source={StaticResource MultiplierCollection}}"
to 至
ItemsSource="{Binding Source={StaticResource MultiplierCollection, NotifyOnTargetUpdated=True, NotifyOnSourceUpdated=True}}"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.