В проекте приходится использовать часть контролов из WinForms. В частности DataGridView. Есть код(это просто пример ошибки)
*.cs файл
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90.
91.
92.
93.
94.
95.
96.
97.
98.
99.
using WinForms = System.Windows.Forms;
using System.Windows.Forms.Integration;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
private System.Windows.Forms.DataGridView dataGrid = new WinForms.DataGridView();
private WindowsFormsHost myHost = new WindowsFormsHost();
public Window1()
{
InitializeComponent();
dataGrid.EditMode = System.Windows.Forms.DataGridViewEditMode.EditOnEnter;
dataGrid.RowHeadersVisible = false;
dataGrid.AutoSizeColumnsMode = WinForms.DataGridViewAutoSizeColumnsMode.DisplayedCells;
dataGrid.Dock = WinForms.DockStyle.Fill;
dataGrid.AutoGenerateColumns = false;
dataGrid.AllowUserToAddRows = false;
dataGrid.AllowUserToResizeColumns = true;
dataGrid.AllowUserToResizeRows = false;
dataGrid.ScrollBars = WinForms.ScrollBars.Both;
dataGrid.SelectionMode = WinForms.DataGridViewSelectionMode.FullRowSelect;
dataGrid.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(dataGrid_CellClick);
dataGrid.DataBindingComplete += new System.Windows.Forms.DataGridViewBindingCompleteEventHandler(dataGrid_DataBindingComplete);
WinForms.DataGridViewTextBoxColumn column1 = new WinForms.DataGridViewTextBoxColumn();
column1.HeaderText = "Номер";
column1.DataPropertyName = "Id";
WinForms.DataGridViewTextBoxColumn column2 = new WinForms.DataGridViewTextBoxColumn();
column2.HeaderText = "Строка";
column2.DataPropertyName = "Name";
dataGrid.Columns.AddRange(new WinForms.DataGridViewColumn[] { column1, column2 });
List<Test> testList = new List<Test>();
for (int i = 0; i < 50; i++)
{
Test test = new Test();
test.Id = i;
test.Name = "Row " + i.ToString();
testList.Add(test);
}
dataGrid.DataSource = testList;
myHost.Child = dataGrid;
myHost.Margin = new Thickness(0, 5, 0, 5);
m_grid.Children.Add(myHost);
Grid.SetRow(myHost, 1);
}
private void dataGrid_DataBindingComplete(object sender, System.Windows.Forms.DataGridViewBindingCompleteEventArgs e)
{
foreach (WinForms.DataGridViewRow row in dataGrid.Rows)
foreach (WinForms.DataGridViewColumn column in dataGrid.Columns)
if (row.Index % 2 == 0)
{
dataGrid.Rows[row.Index].Cells[column.Index].ReadOnly = true;
dataGrid.Rows[row.Index].Cells[column.Index].Style.BackColor = System.Drawing.Color.Gray;
}
}
private void dataGrid_CellClick(object sender, System.Windows.Forms.DataGridViewCellEventArgs e)
{
popupTree.Placement = System.Windows.Controls.Primitives.PlacementMode.RelativePoint;
popupTree.HorizontalOffset = 500;
popupTree.VerticalOffset = 500;
popupTree.StaysOpen = false;
popupTree.Height = 330;
popupTree.Width = 210;
popupTree.IsOpen = true;
}
private void Label_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
popupTree.Placement = System.Windows.Controls.Primitives.PlacementMode.RelativePoint;
popupTree.HorizontalOffset = 500;
popupTree.VerticalOffset = 500;
popupTree.StaysOpen = true;
popupTree.Height = 330;
popupTree.Width = 210;
popupTree.IsOpen = true;
}
}
class Test
{
public int Id { get; set; }
public string Name { get; set; }
}
}
*.xaml файл
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid Name="m_grid">
<Grid.RowDefinitions>
<RowDefinition Height="40"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Label Grid.Row="0" Width="100" Height="32" Content="qqqqqqqqqqqqqqqq" MouseLeftButtonDown="Label_MouseLeftButtonDown"/>
<Popup Visibility="Hidden" Name="popupTree">
<TreeView Margin="0">
<TreeViewItem Header="Cold Drinks">
<TreeViewItem Header="Coke"/>
<TreeViewItem Header="Pepsi"/>
<TreeViewItem Header="Orange Juice"/>
<TreeViewItem Header="Milk"/>
<TreeViewItem Header="Iced Tea"/>
<TreeViewItem Header="Mango Shake"/>
</TreeViewItem>
</TreeView>
</Popup>
</Grid>
</Window>
Если кликаем на метке(WPF контрол) то в появившемся меню можно выбирать любой элемент дерева. А если кликаем на ячейку(WinForm контрол) то нет возможности выбрать элемент. Дерево игнорит выделения. В чем проблема?
я не волшебник, я только учусь...