定制模板组合框,带特殊非模板项目

时间:2013-01-17 13:03:06

标签: wpf combobox rad-controls radcombobox

我有一个RadTreeView,每个项目都有一个带有一些元素的RadCombobox。现在我需要在每个组合框中添加一些“特殊”项。用户可以单击此项以在组合框中添加新元素: enter image description here

我目前的代码:

<DataTemplate x:Key="Monitor">
        <Grid Height="Auto" Width="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="16" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>
            <Image Grid.Column="0" Height="16" Width="16" Source="icons\monitor.png" />
            <TextBlock Text="{Binding Name}" Margin="5 0 0 0" Grid.Column="1" Width="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Center"/>

            <!-- PROBLEM IS HERE -->
            <telerik:RadComboBox Name="RadComboSchedule"
                 Grid.Column="2"
                 Margin="10 0 0 0"
                 Width="155"
                 ItemsSource="{Binding Source={StaticResource DataSource}, Path=ScheduleDataSource}"
                 ItemTemplate="{StaticResource ComboBoxTemplate}"
            >

            </telerik:RadComboBox>
            <Button Name="BtnRemoveMonitor" Grid.Column="3" Style="{StaticResource ButtonListBoxItemStyle}"  Template="{StaticResource RemoveButtonTemplate}" />
        </Grid>
    </DataTemplate>


<HierarchicalDataTemplate x:Key="Group"
           ItemTemplate="{StaticResource Monitor}"
           ItemsSource="{Binding Monitors}">
                <TextBlock Text="{Binding Name}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
</HierarchicalDataTemplate>


<telerik:RadTreeView
    Name="RadTreeViewGroups"
    Height="auto"
    Width="auto"
    ItemsSource="{Binding Source={StaticResource DataSource}, Path=GroupsDataSource}"
    ItemTemplate="{StaticResource Group}"
    >
</telerik:RadTreeView>

所以,我喜欢截图没有元素“添加新项目”。 有什么想法吗?

PS使用标准WPF Combobox和TreeView控件不是问题。

1 个答案:

答案 0 :(得分:2)

您可以在DataSource的{​​{1}}中创建一个名为“ADD NEW ITEM”的新项目,并在用户选择时进行处理。

ComboBox

在这个问题中,你可以看到一个更好的例子,每个项目都是一个类的实例,因此处理“添加项目”请求更容易:
Databound WPF ComboBox with 'New...' item


修改 (关于“添加项目”按钮模板)
基于上面的示例

有这个课程

private void SelectItem(object sender, SelectionChangedEventArgs e)
{
    if (e.AddedItems[0].ToString() == "new")
    {
        string newItem = "completely new item";
        dataSource.Add(newItem);
        ((ComboBox)sender).SelectedItem = newItem;
    }
}

您将public class DisplayClass { public string Name { get; set; } public bool IsDummy { get; set; } } 绑定到ComboBox.ItemsSource,就像这样:

ObservableCollection

将“虚拟”项添加到集合

public ObservableCollection<DisplayClass> DataSource { get; set; }

然后用这样的方式处理项目选择:

DataSource.Add(new DisplayClass { Name = "ADD ITEM", IsDummy = true });

要正确显示项目,您需要更改private void SelectItem(object sender, SelectionChangedEventArgs e) { var comboBox = (ComboBox)sender; var selectedItem = comboBox.SelectedItem as DisplayClass; if (selectedItem != null && selectedItem.IsDummy) { //Creating the new item var newItem = new DisplayClass { Name = comboBox.Items.Count.ToString(), IsDummy = false }; //Adding to the datasource DataSource.Add(newItem); //Removing and adding the dummy item from the collection, thus it is always the last on the 'list' DataSource.Remove(selectedItem); DataSource.Add(selectedItem); //Select the new item comboBox.SelectedItem = newItem; } } ,在项目为虚拟时使图像不可见

ComboBox.ItemTemplate
相关问题