是否可以将 Listxaml 中的静态资源?

发布于 2024-10-12 20:08:22 字数 474 浏览 2 评论 0原文

如果我想在代码隐藏中绑定类似组合框的东西,我完全没有问题。类似这样的事情:

List<string> strings = new List<string>();
AddStringsFromDataSourceToList(strings);

comboBox1.ItemSource = strings;

据我所知,在 XAML 中没有快速而肮脏的方法来做到这一点。尽管 wpf 因其超级简单的数据绑定而受到好评,但这种简单的事情似乎在 C# 中更容易做到。有没有比创建 DependencyProperty 包装器并将它们添加为资源更简单的方法,而无需智能感知或所有进入 ObservableCollections 的帮助?我知道这并非不可能,但如果这样一个简单的任务看起来如此深入,我一定会错过一些东西......

编辑:为了澄清,添加动态列表是这里的问题,而不是静态数组。正如许多人指出的那样,手动添加项目非常容易。

If I want to bind something like a combobox in the code-behind I have no problem at all. Something like :

List<string> strings = new List<string>();
AddStringsFromDataSourceToList(strings);

comboBox1.ItemSource = strings;

As far as I can tell, there is no quick and dirty way to do this in XAML. For all of the praise wpf is receiving for its super simple databinding, something this simple seems far easier to just do in C#. Is there an easier way to do this than creating DependencyProperty wrappers and adding them as resources without much help from intellisense or all that goes into ObservableCollections? I understand that its not impossible, but I must be missing something if such a simple task seems so in depth...

EDIT: To clarify, adding dynamic Lists is the issue here, not static arrays. It is super easy to add items manually, as many have pointed out.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

江心雾 2024-10-19 20:08:22
<Window.Resources>
    <x:Array x:Key="strings" Type="sys:String" 
            xmlns:sys="clr-namespace:System;assembly=mscorlib">
            <sys:String>One</sys:String>
            <sys:String>Two</sys:String>
    </x:Array>
    <!-- likewise -->
    <x:Array x:Key="persons" Type="{x:Type local:Person}" 
            xmlns:local="clr-namespace:namespace-where-person-is-defined">
            <local:Person FirstName="Sarfaraz" LastName="Nawaz"/>
            <local:Person FirstName="Prof" LastName="Plum"/>
    </x:Array>
<Window.Resources>


<ComboBox ItemsSource="{StaticResource strings}" />

<ListBox ItemsSource="{StaticResource persons}">
     <ListBox.ItemTemplate>
           <DataTemplate>
               <TextBlock>
                     <Run Text="{Binding FirstName}"/>
                     <Run Text="  "/>
                     <Run Text="{Binding LastName}"/>
               </TextBlock>
           </DataTemplate>
     </ListBox.ItemTemplate>
</ListBox>
<Window.Resources>
    <x:Array x:Key="strings" Type="sys:String" 
            xmlns:sys="clr-namespace:System;assembly=mscorlib">
            <sys:String>One</sys:String>
            <sys:String>Two</sys:String>
    </x:Array>
    <!-- likewise -->
    <x:Array x:Key="persons" Type="{x:Type local:Person}" 
            xmlns:local="clr-namespace:namespace-where-person-is-defined">
            <local:Person FirstName="Sarfaraz" LastName="Nawaz"/>
            <local:Person FirstName="Prof" LastName="Plum"/>
    </x:Array>
<Window.Resources>


<ComboBox ItemsSource="{StaticResource strings}" />

<ListBox ItemsSource="{StaticResource persons}">
     <ListBox.ItemTemplate>
           <DataTemplate>
               <TextBlock>
                     <Run Text="{Binding FirstName}"/>
                     <Run Text="  "/>
                     <Run Text="{Binding LastName}"/>
               </TextBlock>
           </DataTemplate>
     </ListBox.ItemTemplate>
</ListBox>
郁金香雨 2024-10-19 20:08:22

您是否正在寻找这样的东西:

<ComboBox>
    <ComboBox.ItemsSource>
        <x:Array Type="sys:String" 
             xmlns:sys="clr-namespace:System;assembly=mscorlib">
            <sys:String>One</sys:String>
            <sys:String>Two</sys:String>
        </x:Array>
    </ComboBox.ItemsSource>                
</ComboBox>

如果您只想定义列表的某些项目,请查看 Henk Holterman 的解决方案。顺便说一句,您也可以将数组声明为资源和其他类型。

更新

您似乎更改了您的问题。但我现在不明白你的愿望是什么。如果您想绑定代码隐藏中的集合,请创建一个返回此集合的公共属性,将 DataContext 设置为公开此属性的对象,并在 XAML 中定义一个 Binding:

<ComboBox ItemsSource="{Binding NameOfYourCollectionProperty}"...

希望我正确理解您的问题。 。

Are you looking for something like this:

<ComboBox>
    <ComboBox.ItemsSource>
        <x:Array Type="sys:String" 
             xmlns:sys="clr-namespace:System;assembly=mscorlib">
            <sys:String>One</sys:String>
            <sys:String>Two</sys:String>
        </x:Array>
    </ComboBox.ItemsSource>                
</ComboBox>

If you only want to define some items for a list, look at the solution from Henk Holterman. By the way, you can declare the array also as a resource and for other types.

Update

It seems that you have changed your question. However I don't understand now what your desire is. If you want to bind a collection you have in your code-behind, then make a public property that returns this collection, set the DataContext to the object that exposes this property and define a Binding in XAML:

<ComboBox ItemsSource="{Binding NameOfYourCollectionProperty}"...

Hope I understood your question right...

花之痕靓丽 2024-10-19 20:08:22

当然有:

<ComboBox>
    <ComboBoxItem>One</ComboBoxItem>
    <ComboBoxItem>Two</ComboBoxItem>
</ComboBox>

根据您的目标,还有其他几乎同样简单的语法 - 使用资源或内联项目源,或分组数据..甚至 xml 数据。不要因为你尝试的第一件事并不容易而沮丧地举手 - 在我看来,wpf 值得学习。

WPF 之所以受到赞扬,是因为它比 Windows 窗体更容易将视觉效果与行为分离,并且因为它使创建漂亮的视觉效果变得更加容易,而不是因为它使简单的示例变得更加容易。然而,在这种情况下 - 做这个简单的例子更容易。

根据您的编辑您想从哪里提取它们?您不必以任何方式创建依赖属性或可观察集合。一个简单的列表属性就可以了(在这种情况下,我更喜欢在 xaml 中使用 collectionviewsource )。另外,请不要忘记,如果您不喜欢 XAML,则不需要使用所有 XAML。但是,如果您针对 WPF 进行设计,而不是不管它,您会发现很多任务(比如这个)很容易。

Sure there is:

<ComboBox>
    <ComboBoxItem>One</ComboBoxItem>
    <ComboBoxItem>Two</ComboBoxItem>
</ComboBox>

There are other syntaxes depending on your goal that are nearly as simple - using resources or inline itemssources, or grouped data.. even xml data. Don't throw up your hands in frustration because the first thing you tried wasn't easy - wpf is worth the learning curve, in my opinion.

WPF gets praise because it makes separating the visuals from the behavior much easier than windows forms, and because it makes creating nice visual effects much much easier, not because it makes it easier to do trivial examples. However, in this case - it is easier to do the trivial example.

With your edit Where do you want to pull them from? You don't have to create dependency properties or observable collections by any means. A simple list property will do (I prefer to use a collectionviewsource in the xaml in that case). Also, don't forget that you don't need to use all XAML if you hate it. But if you design for WPF instead of in spite of it you'll find a lot of tasks (like this one) easy.

攒一口袋星星 2024-10-19 20:08:22

您可以这样做:

    <ComboBox>
        <ComboBoxItem>one</ComboBoxItem>
        <ComboBoxItem>two</ComboBoxItem>
    </ComboBox>

并且还存在用于在资源部分中声明(和隐式创建)数据的语法。

也许您可以指出一个更完整的场景,以及要求和约束?

You can do:

    <ComboBox>
        <ComboBoxItem>one</ComboBoxItem>
        <ComboBoxItem>two</ComboBoxItem>
    </ComboBox>

And there also exists syntax for declaring (and implicitly creating) data in for example a Resource section.

Maybe you can point out a more full scenario, with the requirements and constraints ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文