相关疑难解决方法(0)

将集合或数组添加到wpf资源字典中

我搜索高低,无法找到答案.我有两个问题

  1. 如何在XAML中创建数组或集合.我有一个数组,我想坚持在那里并绑定到一个组合框.我的第一个想法是将ItemsControl放在资源字典中,但组合框的ItemsSource需要IEnumerable,因此不起作用.

这是我在资源字典中尝试过的,但都不起作用

<ItemsControl x:Key="stateList">
    <sys:String>AL</sys:String>
    <sys:String>CA</sys:String>
    <sys:String>CN</sys:String>
</ItemsControl>
<ItemsControl x:Key="stateList2">
    <ComboBoxItem>AL</ComboBoxItem>
    <ComboBoxItem>CA</ComboBoxItem>
    <ComboBoxItem>CN</ComboBoxItem>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)

这就是我如何绑定它

<ComboBox SelectedValue="{Binding Path=State}" ItemsSource="{Binding Source={StaticResource stateList2}}"  >

                    </ComboBox>
Run Code Online (Sandbox Code Playgroud)

编辑:更新

我得到了第一部分以这种方式工作

 <col:ArrayList x:Key="stateList3">
    <sys:String>AL</sys:String>
    <sys:String>CA</sys:String>
    <sys:String>CN</sys:String>
</col:ArrayList>
Run Code Online (Sandbox Code Playgroud)

但是,我宁愿不使用数组列表,我想使用通用列表,所以如果有人知道如何请让我知道.

编辑更新:我猜XAML对泛型的支持非常有限,所以也许数组列表是我现在能做的最好的,但是我仍然希望第二个问题的帮助,如果有人有一个anser

第2位.我尝试在我的XAML中引用合并的资源字典并遇到问题,因为在Window.resources下我不仅仅是字典,所以它要求我添加x:Key.添加密钥后,系统将无法再找到资源字典中的项目.我不得不将合并的字典移动到Grid.Resources.理想情况下,我想在app.xaml中引用合并的字典,但我有同样的问题

这是一些示例代码.第一部分需要一个x:key来编译,因为我有转换器并且它抱怨每个项目必须有一个键,如果有多个

<UserControl.Resources>
    <win:BooleanToVisibilityConverter x:Key="VisibilityConverter" />
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/ResourcesD.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources>
Run Code Online (Sandbox Code Playgroud)

我不得不改变它

<UI:BaseStep.Resources>
    <win:BooleanToVisibilityConverter x:Key="VisibilityConverter" />             
</UI:BaseStep.Resources>
<Grid>
    <Grid.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/ResourcesD.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Grid.Resources>
</Grid>
Run Code Online (Sandbox Code Playgroud)

谢谢

wpf resourcedictionary

8
推荐指数
1
解决办法
2万
查看次数

标签 统计

resourcedictionary ×1

wpf ×1