WPF DataGrid how to set ColumnType to different type based on bound data?

丶灬走出姿态 提交于 2019-12-03 07:48:39

问题


I have "Preferences" data structure where I have string "Value" field and enum for "Type" field.

Type can be 0-Boolean, 1-Integer, 2-String ...

Depending on value in this Type field I'd like to display "Value" cell different way Checkbox, Textbox, dropdown, etc. So, to make it clear - same column should display different cells depending on data in that row..

I guess I need to employ DataGridTemplateColumn but I never did that and would like some example if possible.

Also, what can I do with XAML and what needs to be done in Code? I guess Value converter will have to be used as well?


回答1:


           <DataGrid ItemsSource="{Binding Items,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" >
            <DataGrid.Columns>
                <DataGridTemplateColumn>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ContentControl x:Name="content" Content="{Binding}" >
                            </ContentControl>
                            <DataTemplate.Triggers>
                                <DataTrigger Binding="{Binding ItemType}" Value="0">
                                    <Setter TargetName="content" Property="ContentTemplate">
                                        <Setter.Value>
                                            <DataTemplate>
                                                <CheckBox IsChecked="{Binding Value,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"></CheckBox>
                                            </DataTemplate>
                                        </Setter.Value>
                                    </Setter>
                                </DataTrigger>
                                <DataTrigger Binding="{Binding ItemType}" Value="1">
                                    <Setter TargetName="content" Property="ContentTemplate">
                                        <Setter.Value>
                                            <DataTemplate>
                                                <TextBox Text="{Binding Value,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"></TextBox>
                                            </DataTemplate>
                                        </Setter.Value>
                                    </Setter>
                                </DataTrigger>
                            </DataTemplate.Triggers>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>

In CodeBehind you have a ObservableCollection Items {get;set;}

public class SimpleClass { public TypeEnum ItemType{get;set;} public object Value {get;set;} }



来源:https://stackoverflow.com/questions/6619967/wpf-datagrid-how-to-set-columntype-to-different-type-based-on-bound-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!