0
votes

So I'm building a silverlight datagrid dynamically (columns and cells) and I need my users to be able to change the column headers. I used a DataTemplate with a TextBox binded to the Header (I think) on the DataGridColumnHeader.ContentTemplate.

<DataTemplate xmlns='http://schemas.microsoft.com/client/2007'>
    <TextBox Text='{Binding}' />
</DataTemplate>

However when I change the textbox's text the actual Header value does not change. I think I need to use 2 way binding but i'm not sure how that would work. I've been trying to wrap my head around silverlight/wpf bindings but am struggling a bit.

I suppose I could use a textbox.textchanged event to update them or something like that. But I think it would be cleaner in xaml.

Any suggestions? I feel like someone must have created a DataGrid with editable column headers.

1
It sounds like you're trying to edit the first row in the data grid and use that as a header, instead of using the column headers? If not, can you show the way you've defined your column headers?Brad Boyce

1 Answers

0
votes

Alright I fixed it. So I just created a string wrapper class like so:

public class SingleString:System.ComponentModel.INotifyPropertyChanged
{
    String _value = "";
    public String Value
    {
        get { return _value; }
        set
        {
            _value = value;
            OnPropertyChanged("Value");
        }
    }

    public SingleString()
    {
    }
    public SingleString(String val)
    {
        Value = val;
    }

    public override string ToString()
    {
        return Value;
    }

    public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propName));
        }
    }
}

Then I used those object types for my column headers. So my header style worked out to be:

    <Style x:Key="TextBoxHeader" TargetType="dataprimitives:DataGridColumnHeader">
        <Setter Property="ContentTemplate">
            <Setter.Value>
                <DataTemplate>
                    <TextBox Text="{Binding Value, Mode=TwoWay}"/>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>

This resulted in the Header values (which are SingleString objects) to have there values changed when the textbox text was changed. Which resulted in correct behavior with my export. Thanks for the help.