Bitmask Databing

T

TD

Is there a way to DataBind controls to a specific Bit in Bitmask? Here is
some sample code of what I am trying to do ... I am trying to bind the
Visible and Enabled properties of a Button to specific bits in a Bitmask
stored in a DataTable.

namespace Test

{

[Flags]

public enum ButtonStateEnum

{

Enabled = 0x001,

Visible = 0x002,

}

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void Form1_Load(object sender, EventArgs e)

{

// Boolean bValue = true;

ButtonStateEnum bValue = ButtonStateEnum.Enabled | ButtonStateEnum.Visible;

DataTable dt = new DataTable();

dt.Columns.Add("Flags", typeof(ButtonStateEnum));

dt.Rows.Add(bValue);

buttonBound.DataBindings.Add("Enabled", dt, "Flags" );

buttonBound.DataBindings.Add("Visible", dt, "Flags");

}

}

}
 
N

Nicholas Paldino [.NET/C# MVP]

TD,

No, you can't do this directly. I would create an adapter class, which
takes the bitmask and then has a number of boolean properties, one for each
bit in the mask. Then, you can set/get the values, and then have a property
which is the value of the assemblied bitmask.

You can have to implement the INotifyPropertyChanged interface, and fire
the PropertyChanged event any time any of the values change.

You then should be able to bind to an instance of that adapter.
 
T

TD

Without posting a bunch of code from my class that implements
INotifyPropertyChanged which is ButtonStateValue, I still don't see how to
be able to bind this to the individual properties inside a DataSet ... or am
I going about this the wrong way?

ButtonStateValue bValue = new ButtonStateValue();

bValue.ButtonState = ButtonStateEnum.Enabled | ButtonStateEnum.Visible;

DataTable dt = new DataTable();

dt.Columns.Add("Flags", typeof(ButtonStateValue));

dt.Rows.Add(bValue);

buttonBound.DataBindings.Add("Enabled", dt, "Flags" );

buttonBound.DataBindings.Add("Visible", dt, "Flags");



Nicholas Paldino said:
TD,

No, you can't do this directly. I would create an adapter class, which
takes the bitmask and then has a number of boolean properties, one for
each bit in the mask. Then, you can set/get the values, and then have a
property which is the value of the assemblied bitmask.

You can have to implement the INotifyPropertyChanged interface, and
fire the PropertyChanged event any time any of the values change.

You then should be able to bind to an instance of that adapter.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

TD said:
Is there a way to DataBind controls to a specific Bit in Bitmask? Here is
some sample code of what I am trying to do ... I am trying to bind the
Visible and Enabled properties of a Button to specific bits in a Bitmask
stored in a DataTable.

namespace Test

{

[Flags]

public enum ButtonStateEnum

{

Enabled = 0x001,

Visible = 0x002,

}

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void Form1_Load(object sender, EventArgs e)

{

// Boolean bValue = true;

ButtonStateEnum bValue = ButtonStateEnum.Enabled |
ButtonStateEnum.Visible;

DataTable dt = new DataTable();

dt.Columns.Add("Flags", typeof(ButtonStateEnum));

dt.Rows.Add(bValue);

buttonBound.DataBindings.Add("Enabled", dt, "Flags" );

buttonBound.DataBindings.Add("Visible", dt, "Flags");

}

}

}
 
N

Nicholas Paldino [.NET/C# MVP]

You aren't going to be able to bind it to the data set. The
INotifyPropertyChanged interface is only going to allow your grid to bind to
that class, and know when changes are made (through the setting of the
flags).

If you want to bind the property that exposes the bitmask to a field in
a data set, then you are going to have to implement the IBindableComponent
interface (it's not too hard). Once you do that, and then you should be
able to bind the data grid to the value properties that expose the bits and
then bind the control to the DataRowView exposed by the data set. This
should provide you with two-way data binding.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


TD said:
Without posting a bunch of code from my class that implements
INotifyPropertyChanged which is ButtonStateValue, I still don't see how to
be able to bind this to the individual properties inside a DataSet ... or
am I going about this the wrong way?

ButtonStateValue bValue = new ButtonStateValue();

bValue.ButtonState = ButtonStateEnum.Enabled | ButtonStateEnum.Visible;

DataTable dt = new DataTable();

dt.Columns.Add("Flags", typeof(ButtonStateValue));

dt.Rows.Add(bValue);

buttonBound.DataBindings.Add("Enabled", dt, "Flags" );

buttonBound.DataBindings.Add("Visible", dt, "Flags");



Nicholas Paldino said:
TD,

No, you can't do this directly. I would create an adapter class,
which takes the bitmask and then has a number of boolean properties, one
for each bit in the mask. Then, you can set/get the values, and then
have a property which is the value of the assemblied bitmask.

You can have to implement the INotifyPropertyChanged interface, and
fire the PropertyChanged event any time any of the values change.

You then should be able to bind to an instance of that adapter.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

TD said:
Is there a way to DataBind controls to a specific Bit in Bitmask? Here
is some sample code of what I am trying to do ... I am trying to bind
the Visible and Enabled properties of a Button to specific bits in a
Bitmask stored in a DataTable.

namespace Test

{

[Flags]

public enum ButtonStateEnum

{

Enabled = 0x001,

Visible = 0x002,

}

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void Form1_Load(object sender, EventArgs e)

{

// Boolean bValue = true;

ButtonStateEnum bValue = ButtonStateEnum.Enabled |
ButtonStateEnum.Visible;

DataTable dt = new DataTable();

dt.Columns.Add("Flags", typeof(ButtonStateEnum));

dt.Rows.Add(bValue);

buttonBound.DataBindings.Add("Enabled", dt, "Flags" );

buttonBound.DataBindings.Add("Visible", dt, "Flags");

}

}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top