C# 如何将fontsize从combobox绑定到datagrid
如何使用fontsize将值从combobox绑定到datagrid?我试过了,但没用C# 如何将fontsize从combobox绑定到datagrid,c#,wpf,data-binding,combobox,datagrid,C#,Wpf,Data Binding,Combobox,Datagrid,如何使用fontsize将值从combobox绑定到datagrid?我试过了,但没用 <ComboBox Name="FontSizeComboBox" IsEditable="True" Width="60" SelectedIndex="1" Foreground="White"> <ComboBox.ItemsSource> <x:Array T
<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">
12
14
16
18
20
22
24
26
28
SelectedIndex
顾名思义是一个整数。它没有值
属性。这是用户选择的项目的索引。如果用户选择第四项,它将是整数值3
通常,我会这样说:
FontSize="{Binding SelectedValue, ElementName=FontSizeComboBox}"
…但是您的组合框
iasIsEditable=“True”
,因此您希望使用文本
SelectedValue
只能包含列表中的值,但是Text
可以是用户键入的任何内容
另外,如果要绑定到SelectedValue
,还需要将字体大小项目类型更改为double
,因为FontSize
属性上的绑定可以自动从字符串转换,但不能从Int32
(当然,ComboBox.Text
是字符串):
12
14
16
18
20
22
24
26
28
谢谢!它起作用了!如果我正在通过组合框中的剪贴板更改大小,如何使用datagrid进行绑定?@DonKiHot先生,我的错。更新了我的答案。
FontSize="{Binding SelectedValue, ElementName=FontSizeComboBox}"
<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>