XAML 模板化控件如何编写类似 SelectedItem 的内容
XAML Templated Control how to write somthing like SelectedItem
我正在通过模板控件创建我的第一个自定义控件。现在它工作正常,但我不知道如何在我的 table:
中获取 SelectedItem
<Style TargetType="local:DiaryControl">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:DiaryControl">
<Border
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<ListBox ItemsSource="{Binding Path=Students, RelativeSource={RelativeSource TemplatedParent}}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Width="Auto" HorizontalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid Column="0" Margin="20,0,10,0">
<TextBlock VerticalAlignment="Center" Text="{Binding Id}"/>
</Grid>
<Grid Column="1" Margin="0,0,20,0">
<TextBlock VerticalAlignment="Center" Text="{Binding FullName}"/>
</Grid>
<Grid Column="2" Margin="0,0,20,0">
<ComboBox VerticalAlignment="Center" Height="20" FontSize="18" SelectedIndex="{Binding Reason, Mode=TwoWay}">
<ComboBoxItem Content="1"/>
<ComboBoxItem Content="2"/>
<ComboBoxItem Content="3"/>
</ComboBox>
</Grid>
...
<Grid Column="5" Margin="0,0,20,0">
<Button VerticalAlignment="Center" Content="{Binding Comment}"/>
</Grid>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
例如,当我触摸生成的 table 中的 TextBox、TextBlock 等时,我想要控件中的 SelectedItem 之类的东西,以对所选对象执行操作。
谁能帮忙?我在哪里可以了解它?
首先我需要将 ICommand 添加到 Students 集合中的 class Student
public ICommand Process
{
get
{
return new RelayCommand<object>((arg) =>
{
Student button = arg as Student;
----do something
}
});
}
并在 XAML 中:Command="{Binding Process}", CommandParameter="{Binding}"
我正在通过模板控件创建我的第一个自定义控件。现在它工作正常,但我不知道如何在我的 table:
中获取 SelectedItem<Style TargetType="local:DiaryControl">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:DiaryControl">
<Border
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<ListBox ItemsSource="{Binding Path=Students, RelativeSource={RelativeSource TemplatedParent}}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Width="Auto" HorizontalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid Column="0" Margin="20,0,10,0">
<TextBlock VerticalAlignment="Center" Text="{Binding Id}"/>
</Grid>
<Grid Column="1" Margin="0,0,20,0">
<TextBlock VerticalAlignment="Center" Text="{Binding FullName}"/>
</Grid>
<Grid Column="2" Margin="0,0,20,0">
<ComboBox VerticalAlignment="Center" Height="20" FontSize="18" SelectedIndex="{Binding Reason, Mode=TwoWay}">
<ComboBoxItem Content="1"/>
<ComboBoxItem Content="2"/>
<ComboBoxItem Content="3"/>
</ComboBox>
</Grid>
...
<Grid Column="5" Margin="0,0,20,0">
<Button VerticalAlignment="Center" Content="{Binding Comment}"/>
</Grid>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
例如,当我触摸生成的 table 中的 TextBox、TextBlock 等时,我想要控件中的 SelectedItem 之类的东西,以对所选对象执行操作。
谁能帮忙?我在哪里可以了解它?
首先我需要将 ICommand 添加到 Students 集合中的 class Student
public ICommand Process
{
get
{
return new RelayCommand<object>((arg) =>
{
Student button = arg as Student;
----do something
}
});
}
并在 XAML 中:Command="{Binding Process}", CommandParameter="{Binding}"