开发者

In a WPF DataGrid with DataView behind we want to show a ComboBox as a Label until clicked

开发者 https://www.devze.com 2023-03-26 17:46 出处:网络
So we have a WPF DataGrid with some ComboBoxes in some of the cells where there is a foreign relation, but this means we have hundreds of ComboBoxes loaded at a time which takes too long. What we woul

So we have a WPF DataGrid with some ComboBoxes in some of the cells where there is a foreign relation, but this means we have hundreds of ComboBoxes loaded at a time which takes too long. What we would like to do is load a label until it is clicked on at which point a ComboBox is loaded, we can easily do this with a TextBox.

The problem is, our comboboxes work and allow the user to change the foreign key value in the column by selecting from a number of display values (e.g. {Car, Dog, Cat}). But before the user clicks on the label, the label displays the foreign key value itself (e.g. {1, 2, 3}).

Any ideas on what we could do? Any help most appreciated!

开发者_运维技巧
<DataGridTemplateColumn Header="Column Name" SortMemberPath="Column Name"> 
    <DataGridTemplateColumn.CellTemplate> 
        <DataTemplate>
            <Label Content="{Binding Path=DataViewBehindColumnName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" /> 
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate> 
    <DataGridTemplateColumn.CellEditingTemplate> 
        <DataTemplate> 
           <ComboBox 
            DataContext="{DynamicResource ResourceKey=ViewModel}" 
            ItemsSource="{Binding Path=ReferenceTableName, 
                         Converter={StaticResource dataViewToListConverter}}" 
            DisplayMemberPath="ReferenceTableDisplayNamesColumn" 
            SelectedValuePath="ReferenceTablePrimaryKeyColumn" 
            SelectedValue="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridRow}}, 
                           Path=Item[DataViewBehindColumnName]}"
            />
        </DataTemplate> 
     </DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>

Kind regards, Fugu


Create a ViewModel per line and there in provide a Property that represents the resolved string value and another property that represents the key. Bind the labels Content-property to the string-property and the combobox SelectedValue to the key-property.

Not MVVM speaking: Create a line item-wrapper that holds all properties that are necessary for the datagrid including one that holds the resolved string-value of your problematic property.


There's two things coming to my mind when I read your post

1) DataGrid Virtualization. This could save you some load.

2a)Build Proxies for your DataObjects with a bool property "ShowData". This is false by default and set to true when the control is clicked. You could do that with a commandBinding. If you're not using ORM, you can extend your DataSet SourceCode and add this functionality. This could end up being a sh*tload of work, though.

2b)ComboBoxes have an editable Template and a Non-Editable Template(You can find them easily in Blend).Bind the non-Editable Template's content to display a static String and the editable one to the relation. This will show your relation only when you try to edit the selectedItem. Again, if you use ORM like NHibernate, you can profit from LazyLoading features.

0

精彩评论

暂无评论...
验证码 换一张
取 消