This is a migrated thread and some comments may be shown as answers.

Get IsVisible binding of specific column

5 Answers 76 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Adrian
Top achievements
Rank 1
Adrian asked on 12 Apr 2013, 01:44 PM
Hi,

We have a grid that has the columns defined in the XAML. Some of the columns have their IsVisible property bound to a flag in the VM causing it to show/hide depending on the data the grid is displaying. The issue we are currently running into is that we have just added a column list selector for the user so that they can customize which columns are displayed. This works fine, but what happens is when they do this, it obviously sets the IsVisible property of the column and, in turn, overrides any binding that was initially set via XAML. I'd like to be able to "refresh" the grid so that I could default back to the initial state, but i'm not sure how to do that. I have tried the Rebind method, but that doesnt seem to recheck against the current data (or the binding at that point is already out of sync). I was wondering if it is possible, in code, to access the binding, if it exists, of each column. That way, I could explicitly reset the bindings when they user selects the "Restore to Defaults" button on the grid. 

I hope that makes sense.

Thanks

5 Answers, 1 is accepted

Sort by
0
Adrian
Top achievements
Rank 1
answered on 12 Apr 2013, 03:58 PM
UPDATE

part of the issue has been resolved by simply setting the UpdateSourceTrigger in the XAML. But now there is another issue...

Backtracking, our grid has a "Restore Defaults" button in which we are utilizing the PersistenceManager. We also have a column whose header changes when a flag is flipped on the view model.

<telerikGridView:GridViewDataColumn Width="79"
          DataMemberBinding="{Binding Interests}" TextWrapping="Wrap"
          Header="{Binding Source={StaticResource dataContextProxy},Path=DataSource.IsStrategy, Converter={StaticResource StrategyOrGoalHeaderConverter},UpdateSourceTrigger=PropertyChanged}" >
       <telerikGridView:GridViewColumn.CellTemplate>
               <DataTemplate>
                            ........


What is currently happening now, is that when we are reapplying a saved "setting" to the grid, all the binding on the column goes away...the header never changes, and the converter is never hit again.

Here is the code we are using to save the state:

public virtual void ReadState(RadGridViewApplicationSettings settings = null)
{
    // The option to populate a settings object other than the default internal object.
    if (settings == null) settings = Settings;
 
    if (GridView != null)
    {
        PersistenceManager pm = new PersistenceManager();
        Stream stream = pm.Save(GridView);
        stream.Position = 0L;
        var sr = new StreamReader(stream);
        settings.GridSettings = sr.ReadToEnd();
        settings.FrozenColumnCount = GridView.FrozenColumnCount;
    }
}

and this is the code that restores to the default:

public virtual void ApplyState(RadGridViewApplicationSettings settings = null)
{
    // The option to populate a settings object other than the default internal object.
    if (settings == null) settings = Settings;
 
    if (this.GridView != null)
    {
        if (!string.IsNullOrEmpty(settings.GridSettings))
        {
            byte[] byteArray = Encoding.UTF8.GetBytes(settings.GridSettings);
            MemoryStream stream = new MemoryStream(byteArray);
            stream.Position = 0L;
            var pm = new PersistenceManager();
            pm.Load(GridView, stream);
        }
    }
}

Everything else with the grid is correct, but the Header for that one particular column never changes again.

Any help would be appreciated.

Thanks!!
0
Adrian
Top achievements
Rank 1
answered on 12 Apr 2013, 06:12 PM
Just for testing, I decided to create a duplicate column that simply had a different header ("Goal" and "Strategy") and bound their visibility to the IsStrategy property (one directly and the other through a converter, so that only one shows by default). But, oddly enough, now, when I go restore the defaults, it renames the Goal column to "Strategy", so i'm left with two "Strategy" columns.

I'm confused. 
0
Dimitrina
Telerik team
answered on 17 Apr 2013, 10:57 AM
Hello,

Would you please send me a small demo project showing the exact case you have? That way I can debug it locally in order to see what is going on.
 

All the best,
Didie
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Adrian
Top achievements
Rank 1
answered on 17 Apr 2013, 12:48 PM
It looks like the previous issue was because I forgot to set the UniqueName properties...

As for the PersistenceManager issues, to get around that I had to explicitly reset the bindings in code when the original state is brought back.

System.Windows.Data.Binding bindHeader = new System.Windows.Data.Binding();
bindHeader.Source = this.DataContext;
bindHeader.Path = new PropertyPath("IsStrategy");
bindHeader.Converter = new STP.Common.Converters.StrategyOrGoalHeaderConverter();
bindHeader.UpdateSourceTrigger = System.Windows.Data.UpdateSourceTrigger.PropertyChanged;
System.Windows.Data.BindingOperations.SetBinding(this.MilestoneDetailsListGrid.Columns["testCol"], 
    GridViewColumn.HeaderProperty, bindHeader);            
  
System.Windows.Data.Binding bindStratVis = new System.Windows.Data.Binding();
bindStratVis.Source = this.DataContext;
bindStratVis.Path = new PropertyPath("IsStrategy");
bindStratVis.UpdateSourceTrigger = System.Windows.Data.UpdateSourceTrigger.PropertyChanged;
System.Windows.Data.BindingOperations.SetBinding(this.MilestoneDetailsListGrid.Columns["DeployTarget"],
    GridViewColumn.IsVisibleProperty, bindStratVis);
System.Windows.Data.BindingOperations.SetBinding(this.MilestoneDetailsListGrid.Columns["Intention"],
    GridViewColumn.IsVisibleProperty, bindStratVis);
System.Windows.Data.BindingOperations.SetBinding(this.MilestoneDetailsListGrid.Columns["WhyHow"],
    GridViewColumn.IsVisibleProperty, bindStratVis);
System.Windows.Data.BindingOperations.SetBinding(this.MilestoneDetailsListGrid.Columns["TechReq"],
    GridViewColumn.IsVisibleProperty, bindStratVis);
  
System.Windows.Data.Binding bindAssetVis = new System.Windows.Data.Binding();
bindAssetVis.Source = this.DataContext;
bindAssetVis.Path = new PropertyPath("IsStrategy");
bindAssetVis.Converter = new STP.Common.Converters.NotBooleanConverter();
bindAssetVis.UpdateSourceTrigger = System.Windows.Data.UpdateSourceTrigger.PropertyChanged;
System.Windows.Data.BindingOperations.SetBinding(this.MilestoneDetailsListGrid.Columns["AssetName"],
    GridViewColumn.IsVisibleProperty, bindAssetVis);

Is this expected way around this? Is there another way to preserve the binding with the PersistenceManager?
0
Dimitrina
Telerik team
answered on 19 Apr 2013, 03:34 PM
Hello,

You can also check our online demo on how to persist the GridView's settings.  

Kind regards,
Didie
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Tags
GridView
Asked by
Adrian
Top achievements
Rank 1
Answers by
Adrian
Top achievements
Rank 1
Dimitrina
Telerik team
Share this question
or