Frage

Im using a telerik gridview for silverlight and Im trying to programatically set the maximum character length on each individual column after the gridview is populated with data. I want to make it so when the user is inserting or editing a column's cell, the maximum (char) length is set based on a control records max length. So far Ive only been able to set the MaxWidth, but that doesnt help me because the column header is always longer than the atual text allowed, and the MaxWidth sets the column width in pixels, not the columns editing max character length. Can someone point me in the right direction. I can provide some code if need be.

Ive tried setting it to column.CellEditTemplate.SetValue(TextBoxEditor.MaxLengthProperty, MYMAXLENGTH);

but it gives me an error.

War es hilfreich?

Lösung

How about something like this?

Style textBoxStyle = new Style(typeof(TextBox));
textBoxStyle.Setters.Add(new Setter(TextBox.MaxLengthProperty, myMaxLength));
(this.MyGrid.Columns[0] as GridViewBoundColumnBase).EditorStyle = textBoxStyle;

http://www.telerik.com/community/forums/silverlight/gridview/best-way-to-set-maxlength-on-gridviewdatacolumn.aspx

Andere Tipps

I'm not sure what your grid's data context is or where the "control record" is, but perhaps something like this would help?

<telerik:RadGridView ItemsSource="{Binding MyData, Source={StaticResource MyViewModel}}">
    <telerik:RadGridView.Columns>
        <telerik:GridViewDataColumn>
            <telerik:GridViewDataColumn.CellEditTemplate>
                <DataTemplate>
                    <TextBox MaxLength="{Binding MyMaxLength, Source={StaticResource MyViewModel}}" />
                </DataTemplate>
            </telerik:GridViewDataColumn.CellEditTemplate>
        </telerik:GridViewDataColumn>
    </telerik:RadGridView.Columns>
</telerik:RadGridView>

Or, if you're not using a view-model and need to bind to a property of the UserControl, then use ElementName instead of Source on the TextBox binding. (Although you can't really use ElementName unless you also incorporate something like this: ElementName binding inside of a cell template.)

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top