XAML模板化控件如何编写类似于SelectedItem的东西

时间:2015-01-13 10:38:56

标签: c# xaml custom-controls winrt-xaml

我正在通过模板化控件创建我的第一个自定义控件。现在它工作正常,但我不知道如何在我的表中获取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>

例如,当我在生成的表中触摸TextBox,TextBlock等时,我希望控件中的SelectedItem之类的东西能够对所选对象执行操作。

谁可以提供帮助?我可以在哪里了解它?

1 个答案:

答案 0 :(得分:0)

首先,我需要将ICommand添加到学生集合中的班级学生

public ICommand Process
{
    get
    { 
        return new RelayCommand<object>((arg) => 
        {
            Student button = arg as Student;
            ----do something
        }
    });
}

并在XAML中:Command =&#34; {Binding Process}&#34;,CommandParameter =&#34; {Binding}&#34;