WPF列表框绑定ListBoxItem的 [英] WPF ListBox ListBoxItem Binding

查看:1808
本文介绍了WPF列表框绑定ListBoxItem的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要通过萨姆斯书自学WPF在24小时内。在一个点上,作者展示你怎么能一个ListBox的选择项值绑定到一个属性。我得到的,它是pretty简单。但是,当我尝试创建我自己的ListBoxItems我自己的ListBox控件,我似乎无法得到它的工作。

这作品列表框使用系统集合作为其ItemsSource属性:

 < ListBox的X:名称=的FontList
                     DockPanel.Dock =左
                     的ItemsSource ={X:静态Fonts.SystemFontFamilies}
                     WIDTH =160/>

从这个列表框选择的值,然后在一个TextBlock使用如下:

 <的TextBlock
               文本=测试
               的FontFamily ={绑定的ElementName =的FontList,路径=的SelectedItem}
               TextWrapping =包装
               保证金=0 0 0 4/>

请注意,该路径设置为的SelectedItem。

现在,我想使用包含3种不同尺寸的另一个列表框设置字号。下面是我所做的:

 < ListBox的X:名称=大小>
        &所述; ListBoxItem的大于10&下; / ListBoxItem的>
        &所述;一个ListBoxItem→15&下; / ListBoxItem的>
        &所述; ListBoxItem的> 20℃/ ListBoxItem的>
    < /列表框>

然后我添加了一个绑定到文本框的大小属性如下:

 <的TextBlock
               文本=测试
               的FontFamily ={绑定的ElementName =的FontList,路径=的SelectedItem}
               大小={绑定的ElementName =大小,路径=的SelectedItem}
               TextWrapping =包装
               保证金=0 0 0 4/>

当我运行程序的大小不会改变。所以,我想补充我使用尺寸到文本属性绑定 - 为了看它的值:

 <的TextBlock
               文本={绑定的ElementName =大小,路径=的SelectedItem}
               的FontFamily ={绑定的ElementName =的FontList,路径=的SelectedItem}
               大小={绑定的ElementName =大小,路径=的SelectedItem}
               TextWrapping =包装
               保证金=0 0 0 4/>

我看到我点击列表框的尺寸,它是变化的,但我也看到的SelectedItem正在显示,因为这(当我点击进入15)
System.Windows.Controls.ListBoxItem:15

我的问题:
1)什么是所谓的SelectedItem路径返回的实际值?它是System.Windows.Controls.ListBoxItem:15还是15?如果不是15,我怎么可以指定返回路径只有15,而不是System.Windows.Controls.ListBoxItem:15

2)为什么的FontFamily的SelectItem工作?我意识到了FontList从字体名称的系统集合来临,但目前还不清楚我为什么ListBox中没有返回ListBoxItems文字的集合。如果我的ListBox的路径引用返回类型ListBoxItem中的的SelectedItem对象,那么我想我可以使用SelectedItem.Value或者类似的东西的道路 - 但它不工作,也没有智能感知帮助我

我想这个例子中的工作,因为这将有助于明确一些误解我。请不要重构的解决方案,以得到它的工作一些其他的方式,除非它是完全不可能的,我有一个参考的路径,这将使我只是选择了我的尺寸ListBoxItem的的数字部分。

感谢您。


解决方案

  

什么是被称为的SelectedItem路径返回的实际价值?


System.Windows.Controls.ListBoxItem:15 (你可以阅读这是与内容设置为15 ListBoxItem的),这就是为什么你的绑定不起作用 - 预计数值,而不是 ListBoxItem的。您可以指定路径 SelectedItem.Content 来完成这项工作。您还可以设置列表框大小到内容的 SelectedValuePath ,并绑定到的SelectedValue 属性,而不是<$的C $ C>的SelectedItem 。

解决方案1:

 &LT; TextBlock的大小={绑定的ElementName =大小,路径= SelectedItem.Content}/&GT;

解决方案2:

 &LT; ListBox的X:名称=大小SelectedValuePath =内容/&GT;
&LT; TextBlock的大小={绑定的ElementName =大小,路径=的SelectedValue}/&GT;


  

为什么的FontFamily的SelectItem工作?


由于该列表框包含字体集合,不ListBoxItems的集合(它们仍然创建的集合中重新present每个项目虽然)。可以实现与字体大小相同的行为,如果你在code定义字体大小的收集和ListBox'es ItemsSource属性绑定到该集合或定义列表框的内容 System.Double集合值直接在XAML:

 &LT; ListBox的X:名称=大小
         的xmlns:系统=CLR的命名空间:系统;装配= mscorlib程序&GT;
    &lt;系统:双大于10&LT; /系统:双&GT;
    &lt;系统:双&GT; 15℃; /系统:双&GT;
    &lt;系统:双&GT; 20℃/系统:双&GT;
&LT; /列表框&GT;

I am going through the Sams book "Teach Yourself WPF in 24 Hours". At one point the authors show how you can bind a ListBox's selected-item value to a property. I get that, it's pretty straightforward. But when I try to create my own ListBox control with my own ListBoxItems, I can't seem to get it to work.

The ListBox that works uses a system collection as its ItemsSource property:

<ListBox x:Name="FontList"
                     DockPanel.Dock="Left"
                     ItemsSource="{x:Static Fonts.SystemFontFamilies}"
                     Width="160" />

The value selected from this ListBox is then used in a TextBlock as follows:

    <TextBlock 
               Text="Test" 
               FontFamily="{Binding ElementName=FontList, Path=SelectedItem}"
               TextWrapping="Wrap"
               Margin="0 0 0 4" />

Notice that the Path is set to SelectedItem.

Now, I wanted to set the FontSize using another ListBox that contains 3 different sizes. Here is what I did:

    <ListBox x:Name="Size" >
        <ListBoxItem>10</ListBoxItem>
        <ListBoxItem>15</ListBoxItem>
        <ListBoxItem>20</ListBoxItem>
    </ListBox>

And then I added a binding to the Size attribute of the TextBox as follows:

    <TextBlock 
               Text="Test" 
               FontFamily="{Binding ElementName=FontList, Path=SelectedItem}"
               Size="{Binding ElementName=Size, Path=SelectedItem}"
               TextWrapping="Wrap"
               Margin="0 0 0 4" />

The Size doesn't change when I run the program. So I tried to add the binding I was using for Size to the Text attribute--in order to see its value:

    <TextBlock 
               Text="{Binding ElementName=Size, Path=SelectedItem}"" 
               FontFamily="{Binding ElementName=FontList, Path=SelectedItem}"
               Size="{Binding ElementName=Size, Path=SelectedItem}"
               TextWrapping="Wrap"
               Margin="0 0 0 4" />

I see that it is changing as I click the Size ListBox, but I also see that the SelectedItem is displaying as this (when I click the 15 entry): System.Windows.Controls.ListBoxItem:15

My questions: 1) What is the actual value being returned by the Path called SelectedItem? Is it "System.Windows.Controls.ListBoxItem:15" or is it "15"? If it's not 15, how can I specify a Path that returns just 15 and not System.Windows.Controls.ListBoxItem:15?

2)Why does the FontFamily SelectItem work? I realize that the FontList is coming from a System collection of font names, but it is unclear to me why the ListBox isn't returning a collection of ListBoxItems as text. If my ListBox's Path reference is returning a SelectedItem object of type ListBoxItem, then I would think I could use a Path of SelectedItem.Value or something like that--but it doesn't work and there is no Intellisense to help me.

I want to get THIS example working because it will help clear-up some misunderstandings I have. Please don't refactor the solution to get it to work some other way unless it's entirely impossible for me to have a Path reference that will give me just the numeric portion of my Size ListBoxItem that is selected.

Thank you.

解决方案

What is the actual value being returned by the Path called SelectedItem?

It is System.Windows.Controls.ListBoxItem:15 (you can read this as "ListBoxItem with content set to 15"), that's why your binding does not work - it expects a numeric value, not ListBoxItem. You can specify Path as SelectedItem.Content to make this work. Also you can set SelectedValuePath of ListBox "Size" to "Content", and bind to SelectedValue property instead of SelectedItem.

Solution 1:

<TextBlock Size="{Binding ElementName=Size, Path=SelectedItem.Content}" />

Solution 2:

<ListBox x:Name="Size" SelectedValuePath="Content" />
<TextBlock Size="{Binding ElementName=Size, Path=SelectedValue}" />

Why does the FontFamily SelectItem work?

Because that ListBox contains a font collection, not a collection of ListBoxItems (they are still created to represent each item in a collection though). You can achieve the same behavior with font sizes if you define collection of font sizes in code and bind ListBox'es ItemsSource property to that collection or define contents of your ListBox as a collection of System.Double values directly in XAML:

<ListBox x:Name="Size"
         xmlns:system="clr-namespace:System;assembly=mscorlib">
    <system:Double>10</system:Double>
    <system:Double>15</system:Double>
    <system:Double>20</system:Double>
</ListBox>

这篇关于WPF列表框绑定ListBoxItem的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆