Can anyone help? I am trying to bind to an XML file using XAML. I got majority o
ID: 3824595 • Letter: C
Question
Can anyone help? I am trying to bind to an XML file using XAML. I got majority of it but I am having a few problems with two things. The first thing, How would I get the text to appear in the combo box along with all the other options to choose from.
<ComboBox x:Name="comboBoxType" Text="{Binding XPath= Nodes/Node/Type}" Grid.Column="1" Grid.Row="5" Margin="0,16"/>
The text will not appear in the combobox. Also how would I add the item sources?
In the xml file it shows <Type>Speech</Type> <!--[Speech, Quest]--> How would I make these options appear in the combo boxes (Speech, Quest).
The last thing Is how would I set up a trigger to disable other text boxes or combo boxes depending on what option is chosen?
Below is my XAML and my XML.
XML:
<?xml version="1.0" encoding="utf-8" ?>
<Dialog>
<TargetNPC>NPC_11234</TargetNPC>
<CameraType>Automatic</CameraType> <!--[None, Automatic, Manual]-->
<Nodes>
<Node>
<ID>N_1</ID>
<Text>Would you like to go on a quest?</Text>
<Type>Speech</Type> <!--[Speech, Quest]-->
<SpeechType>Default</SpeechType><!--[Default, Yell, Narrative, Combat_Callout, Voiced_Player_Emote, Zone_Wide]-->
<Responses>
<Response>
<Text>Sure</Text>
<NextNode>N_2</NextNode>
</Response>
<Response>
<Text>No Thanks</Text>
<NextNode/>
</Response>
<Response>
<Text>Tell me more</Text>
<NextNode>N_3</NextNode>
</Response>
</Responses>
</Node>
<Node>
<ID>N_2</ID>
<Type>Quest</Type>
<InteractionType>Offer</InteractionType><!--[Offer, Advance, Complete, Fail]-->
<Quest>QU_0</Quest>
<Step>QS_0</Step>
<NextNode />
</Node>
<Node>
<ID>N_3</ID>
<Text>It really is a very nice quest, what do you say?</Text>
<Type>Speech</Type>
<SpeechType>Default</SpeechType>
<Responses>
<Response>
<Text>Sure</Text>
<NextNode>N_2</NextNode>
</Response>
<Response>
<Text>No Thanks</Text>
<NextNode/>
</Response>
<Response>
<Text>Still need more information, what will you give me?</Text>
<NextNode>N_4</NextNode>
</Response>
</Responses>
</Node>
<Node>
<ID>N_4</ID>
<Text>Some gold maybe?</Text>
<Type>Speech</Type>
<SpeechType>Default</SpeechType>
<Responses>
<Response>
<Text>Sure</Text>
<NextNode>N_2</NextNode>
</Response>
<Response>
<Text>No Thanks</Text>
<NextNode/>
</Response>
</Responses>
</Node>
</Nodes>
</Dialog>
XAML:
<Window x:Class="Assignment4._2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="900" Width="800
">
<Window.Resources>
<XmlDataProvider x:Name = "XDP" x:Key="XDP" Source="Dialogfile.xml" XPath="Dialog" />
</Window.Resources>
<Grid DataContext="{StaticResource XDP}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="130"/>
<ColumnDefinition Width="140" />
<ColumnDefinition Width="119"/>
<ColumnDefinition Width="140" />
<ColumnDefinition Width="101"/>
<ColumnDefinition Width="130"/>
<ColumnDefinition Width="140" />
<ColumnDefinition Width="119"/>
<ColumnDefinition Width="140" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" MinHeight="65"/>
<RowDefinition Height="Auto" MinHeight="65"/>
<RowDefinition Height="60" MinHeight="20"/>
<RowDefinition Height="Auto" MinHeight="65"/>
<RowDefinition Height="Auto" MinHeight="65"/>
<RowDefinition Height="Auto" MinHeight="62"/>
<RowDefinition Height="Auto" MinHeight="68"/>
<RowDefinition Height="Auto" MinHeight="150"/>
<RowDefinition Height="Auto" MinHeight="150"/>
<RowDefinition Height="Auto" MinHeight="50"/>
</Grid.RowDefinitions>
<Label x:Name="labelTarget" Content="TargetNPC" HorizontalAlignment="Center" Margin="38,0,20,0" Width="72" FontWeight="Bold" />
<Label x:Name="labelCameraType" Content="Camera Type" Grid.Row="1" HorizontalAlignment="Center" Margin="28,25,22,24" Width="80" FontWeight="Bold" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" VerticalAlignment="Center" Padding="-2,5,5,5" Height="16"/>
<Label x:Name="labelID1" Content="ID:" Grid.Row="3" HorizontalAlignment="Center" Margin="30,0" Width="70" FontWeight="Bold" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/>
<Label x:Name="labelType" Content="Type" Grid.Row="5" HorizontalAlignment="Center" Margin="16,0,28,0" Width="86" FontWeight="Bold" VerticalContentAlignment="Center" HorizontalContentAlignment="Center"/>
<Label x:Name="labelNode1" Content="Node 1:
" Grid.Row="2" HorizontalAlignment="Left" Margin="8,12,0,11" Width="102" FontWeight="Bold" VerticalAlignment="Center" Height="42"/>
<Label x:Name="labelSpeechType" Content="Speech Type:" Grid.Row="6" HorizontalAlignment="Center" Margin="18,0,30,0" Width="82" FontWeight="Bold" VerticalContentAlignment="Center"/>
<Label x:Name="labelResponses" Content="Responses:" Grid.Row="7" HorizontalAlignment="Center" Margin="18,0,30,0" Width="82" FontWeight="Bold" VerticalContentAlignment="Center"/>
<Label x:Name="labelNode1Question" Content="Text:" Grid.Row="4" HorizontalAlignment="Center" Margin="18,0,30,0" Width="82" FontWeight="Bold" VerticalContentAlignment="Center" HorizontalContentAlignment="Center"/>
<Label x:Name="labelNextNode" Content="Next Node:" Grid.Row="8" HorizontalAlignment="Center" Margin="18,0,30,0" Width="82" FontWeight="Bold" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" VerticalAlignment="Center"/>
<Label x:Name="labelInteraction" Content="Interaction" Grid.Column="2" Grid.Row="5" HorizontalAlignment="Center" Margin="10,0" Width="99" FontWeight="Bold" HorizontalContentAlignment="Right" VerticalContentAlignment="Center" Padding="5,5,-10,5"/>
<Label x:Name="labelQuest" Content="Quest" Grid.Column="2" Grid.Row="6" HorizontalAlignment="Center" Margin="10,0" Width="99" FontWeight="Bold" HorizontalContentAlignment="Right" VerticalContentAlignment="Center" Padding="5,5,-10,5"/>
<Label x:Name="labelStep" Content="Step" Grid.Column="2" Grid.Row="7" Margin="10,0" Width="99" FontWeight="Bold" HorizontalContentAlignment="Right" VerticalContentAlignment="Center" Padding="5,5,-10,5" VerticalAlignment="Top" Height="76"/>
<TextBox x:Name="textBoxNPC" Text="{Binding XPath= TargetNPC}" Grid.Column="1" Margin="0,18,0,17" TextChanged="textBoxNPC_TextChanged"/>
<ComboBox x:Name="comboboxCamera" Text="{Binding XPath= CameraType}" Grid.Column="1" Grid.Row="1" Margin="0,17,0,18"/>
<TextBox x:Name="texBoxID" Text="{Binding XPath= Nodes/Node/ID}" Grid.Column="1" Grid.Row="3" Margin="0,17,0,18" TextChanged="texBoxID_TextChanged"/>
<TextBox x:Name="textBoxResponse3" Text="{Binding XPath= Nodes/Node/Responses/Response[3]/Text}" Grid.Column="1" Grid.Row="7" Margin="0,20" RenderTransformOrigin="0.5,0.5" Height="30">
<TextBox.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform Y="35"/>
</TransformGroup>
</TextBox.RenderTransform>
</TextBox>
<TextBox x:Name="textBoxResponse1" Text="{Binding XPath= Nodes/Node/Responses/Response[1]/Text}" Grid.Column="1" Grid.Row="7" RenderTransformOrigin="0.5,0.5" Margin="0,20" Height="30">
<TextBox.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform Y="-38"/>
</TransformGroup>
</TextBox.RenderTransform>
</TextBox>
<TextBox x:Name="textBoxResponse2" Text="{Binding XPath= Nodes/Node/Responses/Response[2]/Text}" Grid.Column="1" Grid.Row="7" RenderTransformOrigin="0.5,0.5" Margin="0,20" Height="30">
<TextBox.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform Y="-2"/>
</TransformGroup>
</TextBox.RenderTransform>
</TextBox>
<TextBox x:Name="textBoxText" Text="{Binding XPath= Nodes/Node/Text}" Grid.Column="1" Grid.Row="4" Margin="0,18,36,17" Grid.ColumnSpan="2"/>
<ComboBox x:Name="comboBoxType" Text="{Binding XPath= Nodes/Node/Type}" Grid.Column="1" Grid.Row="5" Margin="0,16"/>
<ComboBox x:Name="comboBoxSpeechType" Text="{Binding XPath= Nodes/Node/SpeechType}" Grid.Column="1" Grid.Row="6" Margin="0,19"/>
<TextBox x:Name="textBoxNN1" Text="{Binding XPath= Nodes/Node/Responses/Response[1]/NextNode}" Grid.Column="1" Grid.Row="8" Margin="0,18" RenderTransformOrigin="0.5,0.5" Height="30">
<TextBox.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform Y="-20"/>
</TransformGroup>
</TextBox.RenderTransform>
</TextBox>
<TextBox x:Name="textBoxNN2" Text="{Binding XPath= Nodes/Node/Responses/Response[2]/NextNode}" Grid.Column="1" Grid.Row="8" Margin="0,18" RenderTransformOrigin="0.5,0.5" Height="30">
<TextBox.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform Y="15"/>
</TransformGroup>
</TextBox.RenderTransform>
</TextBox>
<TextBox x:Name="textBoxNN3" Text="{Binding XPath= Nodes/Node/Responses/Response[2]/Text}" Grid.Column="1" Grid.Row="8" Margin="0,18" RenderTransformOrigin="0.5,0.5" Height="30">
<TextBox.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform Y="50"/>
</TransformGroup>
</TextBox.RenderTransform>
</TextBox>
<ComboBox x:Name="comboBoxInteraction" Grid.Column="3" Grid.Row="5" Margin="0,16"/>
<TextBox x:Name="textBoxQuest" Grid.Column="3" Grid.Row="6" Margin="0,19"/>
<TextBox x:Name="textBoxStep" Grid.Column="3" Grid.Row="7" Margin="0,19,0,98"/>
</Grid>
</Window>
Explanation / Answer
XAML FIle:
<Window x:Class="Assignment4._2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="900" Width="800
">
<Window.Resources>
<XmlDataProvider x:Name = "XDP" x:Key="XDP" Source="Dialogfile.xml" XPath="Dialog" />
</Window.Resources>
<Grid DataContext="{StaticResource XDP}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="130"/>
<ColumnDefinition Width="140" />
<ColumnDefinition Width="119"/>
<ColumnDefinition Width="140" />
<ColumnDefinition Width="101"/>
<ColumnDefinition Width="130"/>
<ColumnDefinition Width="140" />
<ColumnDefinition Width="119"/>
<ColumnDefinition Width="140" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" MinHeight="65"/>
<RowDefinition Height="Auto" MinHeight="65"/>
<RowDefinition Height="60" MinHeight="20"/>
<RowDefinition Height="Auto" MinHeight="65"/>
<RowDefinition Height="Auto" MinHeight="65"/>
<RowDefinition Height="Auto" MinHeight="62"/>
<RowDefinition Height="Auto" MinHeight="68"/>
<RowDefinition Height="Auto" MinHeight="150"/>
<RowDefinition Height="Auto" MinHeight="150"/>
<RowDefinition Height="Auto" MinHeight="50"/>
</Grid.RowDefinitions>
<Label x:Name="labelTarget" Content="TargetNPC" HorizontalAlignment="Center" Margin="38,0,20,0" Width="72" FontWeight="Bold" />
<Label x:Name="labelCameraType" Content="Camera Type" Grid.Row="1" HorizontalAlignment="Center" Margin="28,25,22,24" Width="80" FontWeight="Bold" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" VerticalAlignment="Center" Padding="-2,5,5,5" Height="16"/>
<Label x:Name="labelID1" Content="ID:" Grid.Row="3" HorizontalAlignment="Center" Margin="30,0" Width="70" FontWeight="Bold" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/>
<Label x:Name="labelType" Content="Type" Grid.Row="5" HorizontalAlignment="Center" Margin="16,0,28,0" Width="86" FontWeight="Bold" VerticalContentAlignment="Center" HorizontalContentAlignment="Center"/>
<Label x:Name="labelNode1" Content="Node 1:
" Grid.Row="2" HorizontalAlignment="Left" Margin="8,12,0,11" Width="102" FontWeight="Bold" VerticalAlignment="Center" Height="42"/>
<Label x:Name="labelSpeechType" Content="Speech Type:" Grid.Row="6" HorizontalAlignment="Center" Margin="18,0,30,0" Width="82" FontWeight="Bold" VerticalContentAlignment="Center"/>
<Label x:Name="labelResponses" Content="Responses:" Grid.Row="7" HorizontalAlignment="Center" Margin="18,0,30,0" Width="82" FontWeight="Bold" VerticalContentAlignment="Center"/>
<Label x:Name="labelNode1Question" Content="Text:" Grid.Row="4" HorizontalAlignment="Center" Margin="18,0,30,0" Width="82" FontWeight="Bold" VerticalContentAlignment="Center" HorizontalContentAlignment="Center"/>
<Label x:Name="labelNextNode" Content="Next Node:" Grid.Row="8" HorizontalAlignment="Center" Margin="18,0,30,0" Width="82" FontWeight="Bold" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" VerticalAlignment="Center"/>
<Label x:Name="labelInteraction" Content="Interaction" Grid.Column="2" Grid.Row="5" HorizontalAlignment="Center" Margin="10,0" Width="99" FontWeight="Bold" HorizontalContentAlignment="Right" VerticalContentAlignment="Center" Padding="5,5,-10,5"/>
<Label x:Name="labelQuest" Content="Quest" Grid.Column="2" Grid.Row="6" HorizontalAlignment="Center" Margin="10,0" Width="99" FontWeight="Bold" HorizontalContentAlignment="Right" VerticalContentAlignment="Center" Padding="5,5,-10,5"/>
<Label x:Name="labelStep" Content="Step" Grid.Column="2" Grid.Row="7" Margin="10,0" Width="99" FontWeight="Bold" HorizontalContentAlignment="Right" VerticalContentAlignment="Center" Padding="5,5,-10,5" VerticalAlignment="Top" Height="76"/>
<TextBox x:Name="textBoxNPC" Text="{Binding XPath= TargetNPC}" Grid.Column="1" Margin="0,18,0,17" TextChanged="textBoxNPC_TextChanged"/>
<ComboBox x:Name="comboboxCamera" Text="{Binding XPath= CameraType}" Grid.Column="1" Grid.Row="1" Margin="0,17,0,18">
<ComboBoxItem>None</ComboBoxItem>
<ComboBoxItem>Automatic</ComboBoxItem>
<ComboBoxItem>Manual</ComboBoxItem>
</ComboBox>
<TextBox x:Name="texBoxID" Text="{Binding XPath= Nodes/Node/ID}" Grid.Column="1" Grid.Row="3" Margin="0,17,0,18" TextChanged="texBoxID_TextChanged"/>
<TextBox x:Name="textBoxResponse3" Text="{Binding XPath= Nodes/Node/Responses/Response[3]/Text}" Grid.Column="1" Grid.Row="7" Margin="0,20" RenderTransformOrigin="0.5,0.5" Height="30">
<TextBox.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform Y="35"/>
</TransformGroup>
</TextBox.RenderTransform>
</TextBox>
<TextBox x:Name="textBoxResponse1" Text="{Binding XPath= Nodes/Node/Responses/Response[1]/Text}" Grid.Column="1" Grid.Row="7" RenderTransformOrigin="0.5,0.5" Margin="0,20" Height="30">
<TextBox.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform Y="-38"/>
</TransformGroup>
</TextBox.RenderTransform>
</TextBox>
<TextBox x:Name="textBoxResponse2" Text="{Binding XPath= Nodes/Node/Responses/Response[2]/Text}" Grid.Column="1" Grid.Row="7" RenderTransformOrigin="0.5,0.5" Margin="0,20" Height="30">
<TextBox.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform Y="-2"/>
</TransformGroup>
</TextBox.RenderTransform>
</TextBox>
<TextBox x:Name="textBoxText" Text="{Binding XPath= Nodes/Node/Text}" Grid.Column="1" Grid.Row="4" Margin="0,18,36,17" Grid.ColumnSpan="2"/>
<ComboBox x:Name="comboBoxType" Text="{Binding XPath= Nodes/Node/Type}" Grid.Column="1" Grid.Row="5" Margin="0,16"/>
<ComboBox x:Name="comboBoxSpeechType" Text="{Binding XPath= Nodes/Node/SpeechType}" Grid.Column="1" Grid.Row="6" Margin="0,19">
<ComboBoxItem>Default</ComboBoxItem>
<ComboBoxItem>Yell</ComboBoxItem>
<ComboBoxItem>Narrative</ComboBoxItem>
<ComboBoxItem>Combat_Callout</ComboBoxItem>
<ComboBoxItem>Voiced_Player_Emote</ComboBoxItem>
<ComboBoxItem>Zone_Wide</ComboBoxItem>
</ComboBox>
<TextBox x:Name="textBoxNN1" Text="{Binding XPath= Nodes/Node/Responses/Response[1]/NextNode}" Grid.Column="1" Grid.Row="8" Margin="0,18" RenderTransformOrigin="0.5,0.5" Height="30">
<TextBox.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform Y="-20"/>
</TransformGroup>
</TextBox.RenderTransform>
</TextBox>
<TextBox x:Name="textBoxNN2" Text="{Binding XPath= Nodes/Node/Responses/Response[2]/NextNode}" Grid.Column="1" Grid.Row="8" Margin="0,18" RenderTransformOrigin="0.5,0.5" Height="30">
<TextBox.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform Y="15"/>
</TransformGroup>
</TextBox.RenderTransform>
</TextBox>
<TextBox x:Name="textBoxNN3" Text="{Binding XPath= Nodes/Node/Responses/Response[2]/Text}" Grid.Column="1" Grid.Row="8" Margin="0,18" RenderTransformOrigin="0.5,0.5" Height="30">
<TextBox.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform Y="50"/>
</TransformGroup>
</TextBox.RenderTransform>
</TextBox>
<ComboBox x:Name="comboBoxInteraction" Grid.Column="3" Grid.Row="5" Margin="0,16"/>
<TextBox x:Name="textBoxQuest" Grid.Column="3" Grid.Row="6" Margin="0,19"/>
<TextBox x:Name="textBoxStep" Grid.Column="3" Grid.Row="7" Margin="0,19,0,98"/>
</Grid>
</Window>