DataGrid列中的Silverlight HyperlinkBut​​ton。必须点击两次

时间:2011-07-21 13:45:14

标签: .net silverlight events

我有一个包含超链接按钮的datagrid列。您必须单击该按钮两次才能使按钮执行它应该执行的操作。我认为第一次点击实际上选择了行。

我相信事件可能会发生在事件没有冒泡(或向下?)到超链接按钮的地方。

想法?

修改

这是xaml:

    <sdk:DataGrid Grid.Row="1" x:Name="workflowsGrid" Margin="6,20,6,0" ItemsSource="{Binding FilteredSource,ElementName=workflowsFilter}"
          AutoGenerateColumns="False" SelectedItem="{Binding SelectedWorkflow,Mode=TwoWay}"
              SelectionChanged="workflowsGrid_SelectionChanged">
    <sdk:DataGrid.Columns>
        <sdk:DataGridTextColumn Header="Name" Binding="{Binding Description}"/>          
        <sdk:DataGridTemplateColumn Header="Action" >
            <sdk:DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <ContentControl Content="{Binding Converter={StaticResource actionConverter}}"/>
                </DataTemplate>
            </sdk:DataGridTemplateColumn.CellTemplate>
        </sdk:DataGridTemplateColumn>
    </sdk:DataGrid.Columns>
</sdk:DataGrid>

这是添加超链接按钮的转换器:

/// <summary>
/// Dynamically controls the action cell in the workflows grid
/// </summary>
protected class ActionValueConverter : IValueConverter
{
    private WorkflowManager _page;
    public ActionValueConverter(WorkflowManager page)
    {
        _page = page;
    }

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var workflow = (WorkflowInstance)value;
        if (workflow.Status == "Complete")
        {
            // create hyperlink buttons for each action that the workflow supports
            var btns = workflow.Definition.Actions
                .Select(x =>
                {
                    HyperlinkButton btn = new HyperlinkButton
                    {
                        Tag = Tuple.Create(workflow, x.Key),
                        Content = x.Value,
                    };
                    btn.Click += new RoutedEventHandler(_page.ActionButton_Click);
                    return btn;
                });

            // stack panel to contain all the buttons
            StackPanel sp = new StackPanel { Orientation = Orientation.Vertical };
            foreach (var btn in btns)
                sp.Children.Add(btn);

            return sp;
        }
        else if (workflow.Status == "In Progress")
        {
            // create only a cancel hyperlink button
            HyperlinkButton btnCancel = new HyperlinkButton { Content = "Cancel", Tag = Tuple.Create(workflow, "Cancel") };
            btnCancel.Click += new RoutedEventHandler(_page.ActionButton_Click);
            return btnCancel;
        }
        else
        {
            throw new Exception("workflow status not supported: " + workflow.Status);
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

1 个答案:

答案 0 :(得分:2)

您可以使用编辑模板而不是以下链接中指定的Cell模板:

http://forums.silverlight.net/p/132619/296134.aspx

您的链接列始终处于“编辑”模式,只需单击一下即可。

编辑:

<sdk:DataGridTemplateColumn.CellEditingTemplate >

而不是

<sdk:DataGridTemplateColumn.CellTemplate>

希望它有所帮助!