DataGrid를 채우는 데 사용하는 사용자 정의 컨트롤이 있습니다.DataGrid를 컨트롤에서 편집하는 방법?
사용자가 하단의 빈 행을 편집하여 항목을 추가 할 수 있기를 바랍니다. (이것이 내가 itemscontrol보다는 DataGrid를 사용하는 이유입니다.) 그러나 DataGrid는 사용자가 컨트롤의 배경을 클릭하지 않는 한 마지막 항목이 편집되었음을 인식하지 못합니다. 사용자가 컨트롤이 노출하는 속성을 변경할 때 새 항목을 추가하고 싶습니다. 컨트롤의
XAML :
<UserControl x:Class="ControlTest.MyControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:ControlTest"
mc:Ignorable="d"
d:DesignHeight="50" d:DesignWidth="300"
DataContext="{Binding Path=., Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
>
<StackPanel Orientation="Vertical">
<TextBox Text="{Binding Path=p1, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
Width="300"
Height="30"
VerticalAlignment="Center"/>
<ComboBox ItemsSource="{Binding Path=DropDownValues,
RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType=local:MyControl}}"
SelectedItem="{Binding Path=p2, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
Height="30"/>
</StackPanel>
</UserControl>
CS :
public partial class MyControl : UserControl
{
private static readonly DependencyProperty DropDownValuesProperty =
DependencyProperty.Register(
"DropDownValues",
typeof(List<String>),
typeof(MyControl),
new FrameworkPropertyMetadata(new List<String>()
));
public List<String> DropDownValues
{
get
{
return (List<String>)GetValue(DropDownValuesProperty);
}
set
{
SetValue(DropDownValuesProperty, value);
}
}
public MyControl()
{
InitializeComponent();
}
}
데이터 그리드 XAML은
<DataGrid
AutoGenerateColumns="False"
ItemsSource="{Binding objs, Mode=TwoWay}"
HeadersVisibility="None"
Margin="0,0,0.4,0"
CanUserAddRows="True"
>
<DataGrid.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</DataGrid.ItemsPanel>
<DataGrid.Columns>
<DataGridTemplateColumn Width="300">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate DataType="local:Measure">
<local:MyControl
DataContext="{Binding ., Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
DropDownValues=
"{Binding DataContext.list, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"
Width="300"
/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
나는이 작품을 만들 수 및/또는 할 수있는 더 좋은 방법이 이?
당신은 무엇을 의미합니까? 사용자가 컨트롤의 배경을 클릭하지 않으면 마지막 항목이 편집되었음을 알지 못합니다. 나는 기본 동작이 무엇을 의미합니까 ??? – Vishal
@Vishal 컨트롤이없는 기본 동작은 포커스가없는 필드가 새로 추가되면 포커스가 사라지는 것입니다. 이것은 내가 컨트롤을 가지고있을 때 관찰했던 것과 다소 비슷하지만 편집하는 컨트롤 안에있는 컨트롤 중 하나 일 때 셀에 포커스가 있다는 것을 데이터 격자가 이해하지 못한다. 그래서 배경을 클릭 할 때까지 새로운 아이템이 추가되지 않는다. 제어. – Taemyr
Datagrid가 마지막 항목이 편집되었음을 인식하지 못한다는 것을 어떻게 알 수 있습니까? 'Items'에 직접 또는'objs'에 추가하고 있습니까? – M312V