이 질문은 What is it about DataTable Column Names with dots that makes them unsuitable for WPF's DataGrid control?과 관련 있습니다.WPF Datagrid가 머리글 정렬에서 충돌 함
임시 테이블 빌더에 대한 결과를 표시하기 위해 DataGrid를 사용하므로 데이터 테이블 열은 무엇이든 될 수 있습니다.
나는 대부분의 득표 수 (14)로 답변을 따라 갔다. 모든 것이 훌륭하게 작동했지만 헤더를 클릭하여 열을 정렬하면 전체 애플리케이션이 충돌합니다.
오류 :
System.IndexOutOfRangeException HResult=0x80131508 Message=Index was outside the bounds of the array. Source=AppName StackTrace: at AppName.App.ShowUnhandeledException(DispatcherUnhandledExceptionEventArgs e) in <path>\App.xaml.cs:line 51 at AppName.App.AppDispatcherUnhandledException(Object sender, DispatcherUnhandledExceptionEventArgs e) in <path>\App.xaml.cs:line 27 at System.Windows.Threading.Dispatcher.CatchException(Exception e) at System.Windows.Threading.Dispatcher.CatchExceptionStatic(Object source, Exception e) at System.Windows.Threading.ExceptionWrapper.CatchException(Object source, Exception e, Delegate catchHandler) at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler) at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs) at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam) at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg) at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame) at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame) at System.Windows.Application.RunDispatcher(Object ignore) at System.Windows.Application.RunInternal(Window window) at System.Windows.Application.Run(Window window) at System.Windows.Application.Run() at AppName.App.Main()
이 내 XAML입니다 :
<DataGrid x:Name="DgridQueryResults" Grid.Row="1" Background="LightGray" Margin="5" ItemsSource ="{Binding .}" IsReadOnly="True" CanUserAddRows="False" CanUserDeleteRows="False" CanUserResizeColumns="true" CanUserReorderColumns="False" CanUserSortColumns="True" ClipboardCopyMode="ExcludeHeader"SelectionUnit="Cell" CopyingRowClipboardContent="DgridQueryResults_CopyingRowClipboardContent" Grid.ColumnSpan="2"/>
내가 항목 소스가 ItemsSource = 는하지만 여전히 충돌 "{바인딩}"와 같은 곳 이상 시도했다. 다음과 같은 항목 소스를 설정하기 전에
private void DgridQueryResults_ColumnSetup()
{
DgridQueryResults.Columns.Clear();
DgridQueryResults.AutoGenerateColumns = false;
foreach (DataColumn column in _results.Columns)
{
var gridColumn = new DataGridTextColumn()
{
Header = column.ColumnName,
Binding = new Binding("[" + column.ColumnName + "]")
};
DgridQueryResults.Columns.Add(gridColumn);
}
}
I 설치 열을 : 다음과 같이
내가 프로그래밍 괄호를 추가
private void ExecuteSql()
{
if (_dataFormData.GetDataViaReader(_sql))
{
_results = _dataFormData.DT;
DgridQueryResults_ColumnSetup();// <-- column setup
DgridQueryResults.DataContext = _results.DefaultView;
}
else
{
DgridQueryResults.DataContext = null;
}
QueryResults.Focus();
}
열 설정이 잘 작동 주석 처리,하지만 그럴 수 없어 바인딩에 위배되는 열 이름 (예 : 완전 중지 (.))이 있습니다.
또한 XAML에서 user sort를 false로 설정하면 해결되지만 사용자가 데이터를 정렬 할 수는 없습니다.
Framework 4.6.1에 대해 VS 2017 15.5.1을 사용하고 있습니다.
다른 사람이이 문제가 발생합니까? 어떤 도움을 주셔서 감사합니다. 고맙습니다!