2017-05-08 61 views
0

如何將數值與從combobox的fontsize綁定到數據網格?我嘗試過,但它沒有工作如何將fontsize從組合框綁定到數據網格

<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"> 

回答

0
FontSize="{Binding Text, ElementName=FontSizeComboBox}" 

SelectedIndex是,顧名思義,一個整數。它沒有Value屬性。這是用戶選擇的項目的索引。如果用戶選擇第四項,則它將是整數值3

通常情況下,我會這樣說:

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

...但你ComboBox IAS IsEditable="True",所以你要Text代替。 SelectedValue只能有一個在列表中找到了價值,但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> 
+0

謝謝!它正在工作!以及如何做到這一點,如果我通過剪貼板更改大小與combobox綁定與datagrid? –

+0

@DonKiHot先生哎呀,我的壞。更新了我的答案。 –