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

W

Wonko the Sane

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
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
W

Wonko the Sane

..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.
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
L

Linda Liu[MSFT]

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:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.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/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
K

Ken Foskey

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
 

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