wpf组合框默认值为textBlock

wpf组合框默认值为textBlock,wpf,dynamic,combobox,default,Wpf,Dynamic,Combobox,Default,我需要在组合框中添加默认值“select”。我无法将此值添加到数据库中。此位置值是动态的。它根据用户角色显示。我尝试了不同的方法,但都没用。请帮忙 <ComboBox Width="140" ItemsSource="{Binding SecurityContexts, Mode=OneWay}" SelectedItem="{Binding ActiveSecurityContext, Mode=TwoWay}"

我需要在组合框中添加默认值“select”。我无法将此值添加到数据库中。此位置值是动态的。它根据用户角色显示。我尝试了不同的方法,但都没用。请帮忙

<ComboBox Width="140" ItemsSource="{Binding SecurityContexts, Mode=OneWay}"
                      SelectedItem="{Binding ActiveSecurityContext, Mode=TwoWay}"
                      ToolTip="Working Location">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Location}"/>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

背后的代码是 SecurityContext=新的ObservableCollection(_currentUser.ApplicationSecurityContext)

公共接口IApplicationSecurityContext
{
IRole角色{get;}
字符串位置{get;}
IEnumerable预算{get;}
}
公共IApplicationSecurityContext活动安全Context
{
获取{返回此。\u currentUser.ActiveSecurityContext;}
设置
{
if(this.\u currentUser.ActiveSecurityContext!=值)
{
此.u currentUser.ChangeActiveSecurityContext(值);
RaisePropertyChanged(“当前用户”);
LoadData();
}
}
}

您可以使用

你可以这样做。在网格/用户控件/组合框中定义资源:

    <Grid.Resources>
        <CollectionViewSource x:Key="cvs" Source="{Binding Binding SecurityContexts, Mode=OneWay}" />
        <DataTemplate DataType="{x:Type c:SecurityContexts}">
             <TextBlock Text="{Binding Location}"/>
        </DataTemplate>
    </Grid.Resources>

然后,您的组合框itemsource将是:

<ComboBox.ItemsSource>
   <CompositeCollection>
      <ComboBoxItem>
         <TextBlock Text="select"/>
      </ComboBoxItem>
      <CollectionContainer Collection="{Binding Source= {StaticResource cvs}}"/>
   </CompositeCollection>
 </ComboBox.ItemsSource>

它应该会起作用。您还需要为资源中的集合定义datatemplate,以定义项目的显示方式


请注意,c:SecurityContext中的c是定义自定义对象的路径

您可以使用

你可以这样做。在网格/用户控件/组合框中定义资源:

    <Grid.Resources>
        <CollectionViewSource x:Key="cvs" Source="{Binding Binding SecurityContexts, Mode=OneWay}" />
        <DataTemplate DataType="{x:Type c:SecurityContexts}">
             <TextBlock Text="{Binding Location}"/>
        </DataTemplate>
    </Grid.Resources>

然后,您的组合框itemsource将是:

<ComboBox.ItemsSource>
   <CompositeCollection>
      <ComboBoxItem>
         <TextBlock Text="select"/>
      </ComboBoxItem>
      <CollectionContainer Collection="{Binding Source= {StaticResource cvs}}"/>
   </CompositeCollection>
 </ComboBox.ItemsSource>

它应该会起作用。您还需要为资源中的集合定义datatemplate,以定义项目的显示方式


请注意,c中的c:SecurityContext是定义自定义对象的路径

实际操作:拥有一组SecurityContext类,这些类公开一个“Location”属性,该属性应显示为组合框项。您只需要添加一个元素,对吗?很抱歉输入错误。在我之前的评论中,它是“做”而不是“去”。然而,如果你不给我们一些澄清,就很难给你一个答案。也许还可以发布代码,不仅仅是xaml,特别是什么是“SecurityContext”以及由什么组成。谢谢你,请检查添加的代码。组合框将显示位置1,location5等基于用户的角色…我只需要在combobox中添加一个默认值“select”。实际上,您要做的是:拥有一个SecurityContext类集合,这些类公开一个“Location”属性,该属性应显示为combobox项。您只需要添加一个元素,对吗?很抱歉输入错误。在我之前的评论中,它是“做”而不是“去”。然而,如果你不给我们一些澄清,就很难给你一个答案。也许还可以发布代码,不仅仅是xaml,特别是什么是“SecurityContext”以及由什么组成。谢谢你,请检查添加的代码。组合框将根据用户的角色显示位置1、位置5等…我只需要在组合框中添加默认值“select”,非常感谢Daniele。。我试图实现您提供的解决方案…我有问题…它说SecurityContext不存在…我是wpf新手…我想我做错了什么…也许我不够清楚。c:表示应用程序可以找到SecurityContext的路径。因此,您必须在UserControl:xmlns:c=“clr namespace:yourProject.YourClass”中定义类似的内容。非常感谢Daniele。。我试图实现您提供的解决方案…我有问题…它说SecurityContext不存在…我是wpf新手…我想我做错了什么…也许我不够清楚。c:表示应用程序可以找到SecurityContext的路径。因此,您必须在UserControl:xmlns:c=“clr namespace:yourProject.YourClass”中定义类似的内容。