I try to make some databinding involving an x:Array defined in the resources, but somehow, it doesn't work even the code is compiling. In the resources, I define an array of Strings this way:
<x:Array x:Key="ArrayReportType" Type="{x:Type sys:String}">
<sys:String>Energy Export</sys:String>
<sys:String>Cost Center Report</sys:String>
</x:Array>
I've also a collection of objects. One of the property is called "ReportType" and is an integer/enum. So I'd like to make a databinding to show the corresponding string to the ReportType instead of the int/enum. I tried this line, but it doesn't work (the second line causes the pb):
<DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
<DataGridTextColumn Header="Type"
Binding="{Binding Source={StaticResource ArrayReportType}, Path=[{Binding ReportType}]}" />
I'm sure, I'm close to the solution. If I write directly "Path=[1]", then it'开发者_运维问答s correct. During runtime, I receive this error:
System.Windows.Data Error: 40 : BindingExpression path error: '[]' property not found on 'object' ''String[]' (HashCode=14199578)'. BindingExpression:Path=[{Binding ReportType}]; DataItem='String[]' (HashCode=14199578); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
Any help would be appreciated.
Ok to use a DataGridComboBoxColumn?
<DataGridComboBoxColumn Header="Type"
ItemsSource="{Binding Source={StaticResource ArrayReportType}}">
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="{x:Type ComboBox}">
<Setter Property="SelectedIndex" Value="{Binding ReportType}" />
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
<DataGridComboBoxColumn.ElementStyle>
<Style TargetType="{x:Type ComboBox}">
<Setter Property="SelectedIndex" Value="{Binding ReportType}" />
</Style>
</DataGridComboBoxColumn.ElementStyle>
</DataGridComboBoxColumn>
Update
Readonly text cell
<DataGridComboBoxColumn Header="Type"
ItemsSource="{Binding Source={StaticResource ArrayReportType}}"
IsReadOnly="True">
<DataGridComboBoxColumn.ElementStyle>
<Style TargetType="{x:Type ComboBox}">
<Setter Property="SelectedIndex" Value="{Binding ReportType}" />
</Style>
</DataGridComboBoxColumn.ElementStyle>
</DataGridComboBoxColumn>
Maybe you can resolve this with a dedicated converter.
Something like that?
<DataGridTextColumn
Header="Type"
Text="{Binding Converter={StaticResource IndexToArrayItemConverter}, ConverterParameter={StaticResource ArrayReportType}, Path=ReportType}"/>
Converter code skipped for being so obvious
精彩评论