Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

WPF Binding XPath Problem

$
0
0

I am trying to color the first column in my WPF DataGrid based on XmlDataProvider Document data.  Here is partial XML:

<Settings xmlns=""><Charting y_max_max="2000" y_max_min="-1900" y_max_val="-1122" y_range_max="2000" y_range_min="10" y_range_val="500" max_minutes="10" interval_seconds="1" chart_thin_size="1" chart_thick_size="4" HighlightMs="3000" /><PortColors><Item Id="1" Color="DodgerBlue"/><Item Id="2" Color="OrangeRed"/><Item Id="3" Color="SlateGray"/><Item Id="4" Color="LimeGreen"/><Item Id="5" Color="Sienna"/><Item Id="6" Color="Salmon"/><Item Id="7" Color="BlueViolet"/></PortColors><Profiles xmlns="" ActiveProfile="2" ChartInterval="1" SaveInterval="10"><Profile Id="0" Desc="- none -" Port="COM5" GridColor="Black" Baud="9600" DataBits="8" Parity="2" StopBits="1" CmdDelay="50" ReadTimeout="250" Scan="4" ProcInterval="0" SetInterval="0" DecInterval="0" ChartInterval="0" SaveInterval="0"><PIDs></PIDs></Profile><Profile Id="1" Desc="COM5 Profile" Port="COM5" GridColor="Blue" Baud="9600" DataBits="8" Parity="2" StopBits="1" CmdDelay="50" ReadTimeout="250" Scan="5" ProcInterval="10" SetInterval="5000" DecInterval="30000" ChartInterval="0" SaveInterval="0"><PIDs><PID Address="1" Port="COM5" Chart="True" LineColor="#FF4386D8" DecPlaces="1" ProcVal="-147" SetPoint="410" /><PID Address="2" Port="COM5" Chart="True" LineColor="#FFFF9A2E" ProcVal="-1451" SetPoint="4200" DecPlaces="0" /><PID Address="3" Port="COM5" Chart="True" LineColor="#FFDB443F" ProcVal="-1459" SetPoint="4300" DecPlaces="0" /><PID Address="4" Port="COM5" Chart="True" LineColor="#FFA8D44F" ProcVal="-1459" SetPoint="4400" DecPlaces="0" /></PIDs></Profile><Profile Id="2" Desc="COM3 Profile" Port="COM3" GridColor="Red" Baud="9600" DataBits="8" Parity="2" StopBits="1" CmdDelay="50" ReadTimeout="250" Scan="5" ProcInterval="10" SetInterval="5000" DecInterval="30000" ChartInterval="0" SaveInterval="0"><PIDs><PID Address="1" Port="COM3" Chart="True" LineColor="#FF4386D8" DecPlaces="1" ProcVal="-147" SetPoint="410" /><PID Address="2" Port="COM3" Chart="True" LineColor="#FFFF9A2E" ProcVal="-1451" SetPoint="4200" DecPlaces="0" /><PID Address="3" Port="COM3" Chart="True" LineColor="#FFDB443F" ProcVal="-1459" SetPoint="4300" DecPlaces="0" /><PID Address="4" Port="COM3" Chart="True" LineColor="#FFA8D44F" ProcVal="-1459" SetPoint="4400" DecPlaces="0" /></PIDs></Profile></Profiles>

Here is (partial) XAML for my DataGrid:

<DataGrid Name="dgPIDData" Margin="200,20,0,0" AutoGenerateColumns="False" Height="590" Width="275" HorizontalAlignment="Left" VerticalAlignment="Top" 
                  DataContext="{Binding Source={StaticResource MySettings}}" ItemsSource="{Binding XPath=//PID}"><DataGrid.Columns><DataGridTextColumn x:Name="Port" HeaderStyle="{StaticResource DGCenterColHeadStyle}" Width="50" Header="Port" Binding="{Binding XPath=../../@Port}"><DataGridTextColumn.ElementStyle><Style TargetType="{x:Type TextBlock}"><Setter Property="TextAlignment" Value="Center"/><Setter Property="Foreground" Value="{Binding FallbackValue=Gold, XPath=//PortColors/Item[@Id\=./@Id]/@Color}"/><!--<Setter Property="Foreground" Value="{Binding FallbackValue=Black, XPath=../../@GridColor}"/>--></Style></DataGridTextColumn.ElementStyle></DataGridTextColumn>

Based on the XML snippet above, I end up with 8 rows in my DataGrid, the first 4 rows having column 1 text of "COM5", and the next 4 having column 1 text of "COM3".  This part is working just fine.

Now I want the "COM5" text in column 1 to be in one color on its 4 rows, and the "COM3" text to be in another color on its 4 rows.  If I use the commented Setter above for "Foreground", it works properly.  The XPath causes it to obtain the color for column 1 from the GridColor attribute of the particular PID element's grandparent node.  This will work properly, but requires me to put color names in each and every "Profile" element, which I'd rather not do.

If I instead attempt to reference the proper PortColors Item element in my XPath (by using the un-commented Setter above for "Foreground"), it does not work, and all 8 grid rows end up having the same column-1 color.

It makes more sense to use the PortColors section of my XML, if I could get the XPath to work.  I've done similar things with XPath before, but can't get this particular one to work.  I know it *partially* works, because if I replace the text inside the square brackets with "@Id\=1", "@Id\=2", etc., it finds the proper color.  Even though it still colors all 8 rows the same, it demonstrates that I can refer to another XML section.

Any ideas how to properly build the XPath so each set of 4 rows can get its color properly?

Thanks...


Ron Mittelman


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>