0

我已经绑定了带有复选框的列表框。

现在我必须在双击它时更改复选框的名称。

如何更改复选框的名称(我必须为用户提供更改复选框名称的选项,这意味着用户将双击复选框的名称,然后名称将被文本框替换,然后用户可以添加名称。在模糊或合适的事件中它将被保存到数据库中)

 <ListBox AlternationCount="2"   Width="140" Margin="18,63,480,24" Name="lstbxCuisines" ItemsSource="{Binding}" >
     <ListBox.ItemTemplate>
        <DataTemplate>
            <CheckBox  Name="chkCuisine" Height="20"  Margin="0,5,0,0" FontSize="12" Tag="{Binding CuisineId}" Content="{Binding Cuisine}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
    <ListBox.Resources>
        <Style  TargetType="{x:Type ListBoxItem}">
            <Style.Triggers>
                <Trigger Property="ItemsControl.AlternationIndex" Value="0">
                    <Setter Property="Background" Value="#ffffff"></Setter>
                </Trigger>
                <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                    <Setter Property="Background" Value="#f1f6fe"></Setter>
                </Trigger>
            </Style.Triggers>
        </Style>
    </ListBox.Resources>
</ListBox>
4

2 回答 2

0

您可以将 MouseDoubleClick-Event 添加到您的 ComboBox:

            <DataTemplate>
                <CheckBox Name="chkCuisine" Height="20"  Margin="0,5,0,0" FontSize="12" Tag="{Binding CuisineId}" Content="{Binding Cuisine}" MouseDoubleClick="chkCuisine_MouseDoubleClick"/>
            </DataTemplate>

在您的事件处理程序中,您可以更改Name如下:

    private void chkCuisine_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        ((ComboBox)sender).Name = "NewName";
    }
于 2013-05-06T07:03:39.587 回答
0

1路

<CheckBox.Style>
<Style TargetType="{x:Type CheckBox}">
    <EventSetter Event="MouseDoubleClick" Handler="CheckBoxDoubleClick"></EventSetter>
</Style>

protected void CheckBoxDoubleClick(object sender, MouseButtonEventArgs e)
    {
        CheckBox chk = e.Source as CheckBox;
        if (chk != null)
        {
            chk.Content = "Content Changed";
        }
    }

另一种方式

<ListBox MouseDoubleClick="ListBox_MouseDoubleClick">

private void ListBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        var chk = FindParentControl<CheckBox>(e.OriginalSource as DependencyObject);
        if (chk != null)
        {
            ((CheckBox)chk).Content = "Content Changed";
        }
    }

private DependencyObject FindParentControl<T>(DependencyObject control)
    {
        if (control == null)
            return null;

        DependencyObject parent = VisualTreeHelper.GetParent(control);
        while (parent != null && !(parent is T))
        {
            parent = VisualTreeHelper.GetParent(parent);
        }

        return parent;
    }
于 2013-05-06T07:13:41.717 回答