By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,028 Members | 1,108 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,028 IT Pros & Developers. It's quick & easy.

WPF: Binding Control (Non-List) to Specific DataTable Row and Colu

P: n/a
Hello,

I have a custom control (not a listbox or other hierarchical or list
control) that I would like to bind to a specific row and column from a
DataTable.

Each row of the datatable will have columns like PropertyName, IsEnabled,
CurrentValue, etc.

In my control, I want to show the CurrentValue of a property with a
PropertyName of "PropertyX" if IsEnabled is true.

I can see binding the table to the control, and having a converter that
takes that binding, and selects a row based on a ConverterParameter that
contains the property name. However, with a dozen or more of these controls
on a page, that could get a bit messy.

Is there a more elegant way of doing this?

Thanks,
WtS
Jul 23 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Wonko,

Instead of binding to the table, why not bind to the row in the
container that has access to the control and the data? This way, you just
set the DataContext of the control to the row itself, and the binding on the
control can work as normal.

You just have to handle assigning the correct rows to the different
controls.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Wonko the Sane" <Wonko the Sa**@nospam.nospamwrote in message
news:9C**********************************@microsof t.com...
Hello,

I have a custom control (not a listbox or other hierarchical or list
control) that I would like to bind to a specific row and column from a
DataTable.

Each row of the datatable will have columns like PropertyName, IsEnabled,
CurrentValue, etc.

In my control, I want to show the CurrentValue of a property with a
PropertyName of "PropertyX" if IsEnabled is true.

I can see binding the table to the control, and having a converter that
takes that binding, and selects a row based on a ConverterParameter that
contains the property name. However, with a dozen or more of these
controls
on a page, that could get a bit messy.

Is there a more elegant way of doing this?

Thanks,
WtS

Jul 23 '08 #2

P: n/a
..why not bind to the row in the container that has access to the control
and the data?

I (almost) see what you mean.
You just have to handle assigning the correct rows to the different controls.
That's where I'm getting lost. Is this something that must be done in
code-behind? It almost seems like I'd lose the benefit of binding at that
point - this table updates frequently, and I'd have to check which property
changed and assign the proper row to the proper control.

Here's some more background information. Currently, the DataContext of the
panel that contains a number of these controls is set to a single row
DataTable with columns for each value that we now want to put into rows. For
example, there are columns named "PropertyXValue", "PropertyXEnabled",
"PropertyYValue", "PropertyYEnabled", etc. Since we are continually adding
properties to this table, it seems that just adding more and more columns
doesn't seem like a good idea. We are essentially interested in "turning
this table on its side," making generic columns with specific rows.
Jul 23 '08 #3

P: n/a
Wonko,

While I suspect that there is a way to do it in XAML, but I am not sure
how you would indicate which row in the data table should be assigned to the
DataContext. You could definitely do this in code-behind as well if there
isn't a way to do it in XAML.

It doesn't seem like a good idea, but the binding is incredibly simple,
as you bind directly to the appropriate value and enabled properties, and it
should just work.

If you turn the table on its side, then you have to figure out WHICH row
to bind to, and then set the values. That seems like more trouble than it
is worth.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Wonko the Sane" <Wonko the Sa**@nospam.nospamwrote in message
news:BA**********************************@microsof t.com...
>..why not bind to the row in the container that has access to the control
and the data?

I (almost) see what you mean.
>You just have to handle assigning the correct rows to the different
controls.

That's where I'm getting lost. Is this something that must be done in
code-behind? It almost seems like I'd lose the benefit of binding at that
point - this table updates frequently, and I'd have to check which
property
changed and assign the proper row to the proper control.

Here's some more background information. Currently, the DataContext of
the
panel that contains a number of these controls is set to a single row
DataTable with columns for each value that we now want to put into rows.
For
example, there are columns named "PropertyXValue", "PropertyXEnabled",
"PropertyYValue", "PropertyYEnabled", etc. Since we are continually
adding
properties to this table, it seems that just adding more and more columns
doesn't seem like a good idea. We are essentially interested in "turning
this table on its side," making generic columns with specific rows.

Jul 23 '08 #4

P: n/a
Hi Wonko,
For example, there are columns named "PropertyXValue",
"PropertyXEnabled", "PropertyYValue", "PropertyYEnabled", etc. Since we
are continually adding properties to this table, it seems that just adding
more and more columns doesn't seem like a good idea.

Why do you need to add more and more columns into the DataTable?
We are essentially interested in "turning this table on its side," making
generic columns with specific rows.

I couldn't understand what you mean in the above sentence. Could you please
explain your problem and what you're going to achieve in detail?

I look forward to your reply!

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Jul 24 '08 #5

P: n/a
On Wed, 23 Jul 2008 08:13:00 -0700, Wonko the Sane wrote:
Hello,

I have a custom control (not a listbox or other hierarchical or list
control) that I would like to bind to a specific row and column from a
DataTable.

Each row of the datatable will have columns like PropertyName,
IsEnabled, CurrentValue, etc.

In my control, I want to show the CurrentValue of a property with a
PropertyName of "PropertyX" if IsEnabled is true.

I can see binding the table to the control, and having a converter that
takes that binding, and selects a row based on a ConverterParameter that
contains the property name. However, with a dozen or more of these
controls on a page, that could get a bit messy.

Is there a more elegant way of doing this?
It sounds like you DO have a list. You have a series of objects indexed
into a hash by the property name. This object can contain a heap of
state info, where on screen for example.

The Update mechanism would then simply look up the appropriate property
entry and update it. It could even use it's own object that supports
ToString so that you can use it generically and do specific actions on it.

Ken
Jul 24 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.