双击时,将列表框中存在的复选框名称更改为项目模板WPF

时间:2013-05-06 04:11:40

标签: c# wpf

我已将列表框中的复选框绑定在其中。

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

如何更改复选框的名称 (我必须给用户选项更改复选框的名称意味着用户将双击复选框的名称,然后名称将被文本框替换,然后用户可以添加名称。模糊或适当的事件将保存到数据库)< / p>

 <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>

2 个答案:

答案 0 :(得分: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>

在您的Eventhandler中,您可以更改Name,如此:

    private void chkCuisine_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        ((ComboBox)sender).Name = "NewName";
    }

答案 1 :(得分: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;
    }