【问题标题】:How to binding fontsize from combobox to datagrid如何将字体大小从组合框绑定到数据网格
【发布时间】:2017-10-06 13:42:22
【问题描述】:

如何将值与字体大小从组合框绑定到数据网格?我试过了,还是不行

  <ComboBox Name="FontSizeComboBox" IsEditable="True" 
        Width="60" SelectedIndex="1"
                  Foreground="White">
            <ComboBox.ItemsSource>
                <x:Array Type="{x:Type System:Int32}">
                    <System:Int32>12</System:Int32>
                    <System:Int32>14</System:Int32>
                    <System:Int32>16</System:Int32>
                    <System:Int32>18</System:Int32>
                    <System:Int32>20</System:Int32>
                    <System:Int32>22</System:Int32>
                    <System:Int32>24</System:Int32>
                    <System:Int32>26</System:Int32>
                    <System:Int32>28</System:Int32>
                </x:Array>
            </ComboBox.ItemsSource>
        </ComboBox>
   <DataGrid Grid.Row="1" x:Name="dgrPoints" ItemsSource="{Binding Stations}" VerticalAlignment="Top" AutoGenerateColumns="False" CanUserResizeColumns="True"
              FontSize="{Binding  Path=SelectedIndex.Value ,ElementName=FontSizeComboBox}" CellEditEnding="dgrPoints_CellEditEnding">

【问题讨论】:

    标签: c# wpf data-binding combobox datagrid


    【解决方案1】:
    FontSize="{Binding Text, ElementName=FontSizeComboBox}"
    

    SelectedIndex,顾名思义,是一个整数。它没有 Value 属性。那是用户选择的项目的索引。如果用户选择第四项,它将是整数值3

    通常,我会这样说:

    FontSize="{Binding SelectedValue, ElementName=FontSizeComboBox}"
    

    ...但是你的ComboBoxIsEditable="True",所以你想要TextSelectedValue 只能有一个在列表中找到的值,但Text 可以是用户输入的任何值。

    另外,如果您绑定到SelectedValue,您还需要将字体大小项目类型更改为double,因为FontSize 属性上的绑定可以自动从字符串转换,但不能从Int32(当然,ComboBox.Text 是一个字符串):

    <ComboBox.ItemsSource>
        <x:Array Type="{x:Type System:Double}">
            <System:Double>12</System:Double>
            <System:Double>14</System:Double>
            <System:Double>16</System:Double>
            <System:Double>18</System:Double>
            <System:Double>20</System:Double>
            <System:Double>22</System:Double>
            <System:Double>24</System:Double>
            <System:Double>26</System:Double>
            <System:Double>28</System:Double>
        </x:Array>
    </ComboBox.ItemsSource>
    

    【讨论】:

    • 谢谢!它正在工作!如果我通过组合框中的剪贴板更改大小并与数据网格绑定,该怎么做?
    • @Mr.DonKiHot 哎呀,我的错。更新了我的答案。
    猜你喜欢
    • 2012-11-27
    • 2017-12-08
    • 2014-03-01
    • 2021-02-16
    • 2011-12-04
    • 2012-06-05
    • 1970-01-01
    • 1970-01-01
    • 2013-05-21
    相关资源
    最近更新 更多