Wpf and x binding: arrays

I am trying to create some data binding that includes an x: Array defined in resources, but somehow it does not work even when compiling the code. In resources, I define an array of strings as follows:

<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 also have a collection of objects. One of the properties is called "ReportType" and is an integer / enumeration. Therefore, I would like to do data binding to show the corresponding line in ReportType instead of int / enum. I tried this line, but it does not work (the second line calls 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 a solution. If I write directly "Path = [1]", then this is correct. At runtime, I get this error:

System.Windows.Data error: 40: BindingExpression path error: property '[]' not found in 'object' '' String [] '(HashCode = 14199578)'. BindingExpression: Path = [{Binding ReportType}]; DataItem = 'String []' (HashCode = 14199578); target element is "TextBlock" (Name = ''); target is "Text" (type "String")

Any help would be greatly appreciated.

+3
source share
2 answers

Good to use 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
Text Box readonly

<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>
+1
source

Perhaps you can solve this with a special converter.

Something like that?

<DataGridTextColumn 
    Header="Type"
    Text="{Binding Converter={StaticResource IndexToArrayItemConverter}, ConverterParameter={StaticResource ArrayReportType}, Path=ReportType}"/>

,

+1

Source: https://habr.com/ru/post/1773772/


All Articles