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

WPF RadGridView Custom Column - Capture & Set Control Properties

3 Answers 128 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Joel Palmer
Top achievements
Rank 2
Joel Palmer asked on 11 Mar 2014, 02:41 AM
I have a two columns with buttons in them:
public class GridViewButtonColumn : Telerik.Windows.Controls.GridViewColumn

Based on the data in the row, I need to enable or disable one of those two buttons.  So, I have a Boolean value on the row but I haven't figured out how to use that value to bind the IsEnabled property of the button. 

Appreciate your help,
Joel

PS.  A code example is always welcome.

public class GridViewButtonColumn : Telerik.Windows.Controls.GridViewColumn
    {
        public event ActionRequest ActionRequestEvent;
 
        private System.Drawing.Bitmap _image = null;
 
        public System.Drawing.Bitmap Image
        {
            get
            {
                return _image;
            }
            set
            {
                if (_image != value)
                {
                    _image = value;
                }
            }
        }
         
        public GridViewButtonColumn()
        {
        }
 
        public GridViewButtonColumn(
            System.Drawing.Bitmap image)
        {
            Image = image;
        }
        /// <summary>
        /// WPF RadGridView Custom Column - Capture & Set Control Properties
        /// </summary>
        /// <param name="cell"></param>
        /// <param name="dataItem"></param>
        /// <returns></returns>
        public override FrameworkElement CreateCellElement(
            GridViewCell cell,
            object dataItem)
        {
            RadButton btn = new RadButton()
            {
                Content = new Image()
                {
                    Source = ImageHelper.LoadBitmap(Image)
                }
            };
            btn.Click += btn_Click;
            return btn;
        }
 
        void btn_Click(
            object sender,
            RoutedEventArgs e)
        {
            try
            {
                ExceptionHelper.TestNull(ActionRequestEvent, "Button Click Event Handler");
 
                Button btn = sender as Button;
                RadGridView grid = btn.ParentOfType<RadGridView>();
                 
                ActionRequestEvent(sender,
                    new ActionRequestEventArgs(
                        new ContextItem()
                        {
                            Control = grid.GetType(),
                            Action = ProcessAction.Start,
                        })
                    {
                        Data = btn.DataContext,
                        Sender = grid
                    });
            }
            catch (Exception ex)
            {
                exceptionHandler(ex);
            }
        }

3 Answers, 1 is accepted

Sort by
0
Nick
Telerik team
answered on 14 Mar 2014, 06:50 AM
Hi Joel,

You just need to set a Binding on the button for the property you need. 

It should look something like: 
button.SetBinding(RadButton.IsEnabledProperty, new Binding("Propertyname"));

Let me know if you need any further information. 

Regards,
Nik
Telerik
 

DevCraft Q1'14 is here! Watch the online conference to see how this release solves your top-5 .NET challenges. Watch on demand now.

 
0
Joel Palmer
Top achievements
Rank 2
answered on 14 Mar 2014, 03:38 PM
Thanks for the reply.  I haven't set binding like this for a while so was foreign.  Okay, so I have 2 buttons.  1 is enabled when my property "IsRunning" is true and one is not enabled for the opposite condition.  As you suggested, I set the first one this way:

btn.SetBinding(RadButton.IsEnabledProperty, new Binding("IsRunning"));

However, how do I set the next one to the opposite of "IsRunning"?  Would I employ a converter?  Or, do I just set another property on the data object that "IsNotRunning" and bind it to that?

As always, code is nice.
Thanks for your help, Joel
0
Nick
Telerik team
answered on 17 Mar 2014, 02:43 PM
Hi Joel,

Both of the suggestions can work in your case, however I would chose the first one (with converter). That way you will have to keep track of only one property instead of updating an additional one on every change in the original property.


Hope this makes sense. 

Regards,
Nik
Telerik
 

DevCraft Q1'14 is here! Watch the online conference to see how this release solves your top-5 .NET challenges. Watch on demand now.

 
Tags
GridView
Asked by
Joel Palmer
Top achievements
Rank 2
Answers by
Nick
Telerik team
Joel Palmer
Top achievements
Rank 2
Share this question
or