I have a databound ComboBox where I cannot seem to set the SelectedItem. I have tried SelectedValue/SelectedValuePath, but have having difficulties.
To explain the scenario, I have a parent ListView which contains ComboBoxes w/in the ListViewItems. The parent ListView and the child ComboBox have the same datasource, but display different data. For example, Extension 2 references Extension 1. In this case I am trying to illustrate that 2 mirrors one. The user needs to be able to change which Extension it points to, itself or any of the others. Other than that it is very simple, but almost have it.
Here the example which you can run from your favorite xaml editor.
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Page.Resources>
<XmlDataProvider x:Key="xmlDataProvider">
<x:XData>
<Extensions xmlns="">
<Extension>
<ExtId>1</ExtId>
<ExtName>Extension 1</ExtName>
<ExtValue>1</ExtValue>
</Extension>
<Extension>
<ExtId>2</ExtId>
<ExtName>Extension 2</ExtName>
<ExtValue>1</ExtValue>
</Extension>
<Extension>
<ExtId>3</ExtId>
<ExtName>Extension 3</ExtName>
<ExtValue>3</ExtValue>
</Extension>
<Extension>
<ExtId>4</ExtId>
<ExtName>Extension 4</ExtName>
<ExtValue>4</ExtValue>
</Extension>
</Extensions>
</x:XData>
</XmlDataProvider>
<!-- Extensions -->
<CollectionViewSource
x:Key="CollectionViewSourceExtensions"
Source="{Binding Source={StaticResource xmlDataProvider}, XPath=Extensions/Extension}" />
</Page.Resources>
<Grid>
<ListView
ItemsSource="{Binding
Source={StaticResource CollectionViewSourceExtensions},
Mode=OneWay}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding XPath=ExtName}" />
<TextBlock Text=" - " />
<TextBlock Text="{Binding XPath=ExtValue}" />
</StackPanel>
<ComboBox
SelectedItem="{Binding XPath=ExtId}"
ItemsSource="{Binding
Source={StaticResource CollectionViewSourceExtensions},
Mode=OneTime}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding XPath=ExtId}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</Page>
Was a simple resolution that took a good deal of time to finally figure out...
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Page.Resources>
<XmlDataProvider x:Key="SamplePeople">
<x:XData>
<Extensions xmlns="">
<Exension>
<Id>1</Id>
<Name>Line Key 1</Name>
<Value>1</Value>
</Exension>
<Exension>
<Id>2</Id>
<Name>Line Key 2</Name>
<Value>1</Value>
</Exension>
<Exension>
<Id>3</Id>
<Name>Line Key 3</Name>
<Value>3</Value>
</Exension>
<Exension>
<Id>4</Id>
<Name>Line Key 4</Name>
<Value>4</Value>
</Exension>
</Extensions>
</x:XData>
</XmlDataProvider>
</Page.Resources>
<Grid>
<ListBox x:Name="PeopleListBox"
DataContext="{Binding Source={StaticResource SamplePeople}}"
ItemsSource="{Binding Mode=Default, XPath=/Extensions/node()}"
IsSynchronizedWithCurrentItem="True">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding XPath=Name}" />
<TextBlock Text=" is " />
<ComboBox
SelectedValue="{Binding XPath=Value}"
ItemsSource="{Binding Mode=Default, XPath=/Extensions/Exension/Id}" >
</ComboBox>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Page>
Related
Based on a selected string value in the combobox I want to either show red/blue datatempalte inside the grid.
Can this be done without a ContentControl?
<UserControl.Resources >
<DataTemplate x:Key="red">
<TextBox Text="red" />
</DataTemplate>
<DataTemplate x:Key="blue">
<TextBox Text="blue" />
</DataTemplate>
</UserControl.Resources>
<ComboBox ??? />
<Grid>
// Show red or blue datatemplate here
</Grid>
Why not use a ContentControl?
To make this work simply i would put the templates in an array, which the ComboBox then can bind to:
<x:Array x:Key="templates" Type="{x:Type DataTemplate}">
<DataTemplate>
<DataTemplate.Resources>
<sys:String x:Key="DisplayString">Red</sys:String>
</DataTemplate.Resources>
<TextBox Text="red" />
</DataTemplate>
<DataTemplate>
<DataTemplate.Resources>
<sys:String x:Key="DisplayString">Blue</sys:String>
</DataTemplate.Resources>
<TextBox Text="blue" />
</DataTemplate>
</x:Array>
<ComboBox Name="combo" ItemsSource="{StaticResource templates}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Resources[DisplayString]}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<Grid>
<ContentControl ContentTemplate="{Binding SelectedItem, ElementName=combo}" />
</Grid>
I have a simple code and I’m trying to bind a TextBlock’s Text property to the ListBox but it doesn’t work!
I was trying to follow the instructions in this site http://msdn.microsoft.com/en-us/magazine/cc163299.aspx
Below is my code:
<StackPanel>
<StackPanel.Resources>
<XmlDataProvider x:Key="MoreColors" XPath="Colors">
<x:XData>
<Colors xmlns="">
<color name="Green"/>
<color name="Blue"/>
</Colors>
</x:XData>
</XmlDataProvider>
</StackPanel.Resources>
<TextBlock Height="23" Name="textBlock1" TextAlignment="Center"
Text="{Binding ElementName=listBox1, Path= SelectedItem.Content, Mode=OneWay}"
Background="{Binding Path=Text, RelativeSource={RelativeSource Self}}" Width="119" />
<ListBox x:Name="listBox1" Width="248" Height="56"
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding Source={StaticResource MoreColors},
XPath=color/#name}">
</ListBox>
</StackPanel>
I wonder how I can fix this problem.
Thanks
Rich
Your data is an XmlAttriute, bind to InnerText
Text="{Binding ElementName=listBox1, Path=SelectedItem.InnerText, Mode=OneWay}"
Replace Path= SelectedItem.Content by Path = SelectedItem.Value
I want to create a listbox that'll be bound to XPath, relative to the other listbox's currently selected item.
It's using XmlDataProvider for the data, and the XML file looks like this:
<Programs>
<Program name="...">
<Step name="..."/>
<Step name="..."/>
</Program>
<Program name="another">
...
</Programs
So, the "parent" listbox is listing out all the programs, while "child" shows only steps from the current program.
What's such a type of binding called?
Here you go. Hope this answers your question.
<Window x:Class="StackOverflow.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:StackOverflow"
xmlns:uc="clr-namespace:StackOverflow.UserControls"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<XmlDataProvider x:Key="xml">
<x:XData>
<Programs xmlns="">
<Program name="Program">
<Step name="Step1"/>
<Step name="Step2"/>
</Program>
<Program name="Program2">
<Step name="Step3"/>
<Step name="Step4"/>
</Program>
</Programs>
</x:XData>
</XmlDataProvider>
</Window.Resources>
<Grid>
<StackPanel>
<ListBox x:Name="parent" ItemsSource="{Binding Source={StaticResource xml}, XPath=Programs/Program}"
Height="100">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding XPath=#name}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<ListBox DataContext="{Binding ElementName=parent, Path=SelectedItem}" ItemsSource="{Binding XPath=Step}"
Height="100">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding XPath=#name}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</Grid>
</Window>
I am trying to make a list box where I have complete control over the look of each item in the list box.
I can make the item horizontally stretch. However, there's this thin sliver of blue to the left of a selected item. (In the picture, the middle item is selected).
Can I make this blue strip go away?
alt text http://img44.imageshack.us/img44/949/boundlistboxdisplay.jpg
Here's the complete code.
<Window x:Class="SimpleListTemplate.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>
<Window.Resources>
<XmlDataProvider x:Key="DcCharacters">
<x:XData>
<Characters xmlns="">
<Character HeroName="Catwoman" Identity="Selina Kyle" />
<Character HeroName="Batman" Identity="Bruce Wayne" />
<Character HeroName="Starman" Identity="Jack Knight" />
</Characters>
</x:XData>
</XmlDataProvider>
</Window.Resources>
<Grid>
<ListBox
ItemsSource="{Binding Source={StaticResource DcCharacters}, XPath=//Characters/*}"
HorizontalContentAlignment="Stretch">
<ListBox.ItemTemplate>
<DataTemplate>
<Label
Content="{Binding XPath=#HeroName}"
Height="40"
VerticalContentAlignment="Center"
Background="LightGreen"
BorderThickness="2"
BorderBrush="DarkGreen"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Window>
Here is an update for your code.
<Window x:Class="SimpleListTemplate.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Window.Resources>
<XmlDataProvider x:Key="DcCharacters">
<x:XData>
<Characters xmlns="">
<Character HeroName="Catwoman" Identity="Selina Kyle" />
<Character HeroName="Batman" Identity="Bruce Wayne" />
<Character HeroName="Starman" Identity="Jack Knight" />
</Characters>
</x:XData>
</XmlDataProvider>
<Style TargetType="{x:Type ListBoxItem}" x:Key="ContainerStyle">
<Setter Property="Padding" Value="0,0,0,0"/>
</Style>
</Window.Resources>
<Grid>
<ListBox
ItemsSource="{Binding Source={StaticResource DcCharacters}, XPath=//Characters/*}"
ItemContainerStyle="{StaticResource ContainerStyle}"
HorizontalContentAlignment="Stretch">
<ListBox.ItemTemplate>
<DataTemplate>
<Label
Content="{Binding XPath=#HeroName}"
Height="40"
VerticalContentAlignment="Center"
Background="LightGreen"
BorderThickness="2"
BorderBrush="DarkGreen"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Window>
This may solve your problem.
I have a TreeView that is built on an XML file and contains a text and an image in each TreeViewItem.
Also, I have a TextBlock and an Image, that I want to bound to the selected TreeViewItem.
How can I do this?
Here is my XAML:
<Window.Resources>
<HierarchicalDataTemplate DataType="Node" ItemsSource ="{Binding XPath=ChildNode}">
<StackPanel Orientation="Horizontal">
<Image Source="{Binding XPath=#Image}"/>
<TextBlock Text="{Binding XPath=#Name}" />
</StackPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="ChildNode" ItemsSource ="{Binding XPath=GrandchildNode}">
<StackPanel Orientation="Horizontal">
<Image Source="{Binding XPath=#Image}" />
<TextBlock Text="{Binding XPath=#Name}" />
</StackPanel>
</HierarchicalDataTemplate>
<DataTemplate DataType="GrandchildNode">
<StackPanel Orientation="Horizontal">
<Image Source="{Binding XPath=#Image}" />
<TextBlock Text="{Binding XPath=#Name}" />
</StackPanel>
</DataTemplate>
<XmlDataProvider x:Key="xmlNodeList" Source="XMLFile1.xml" XPath="Root"/></Window.Resources><StackPanel>
<TreeView Name="treeView1" ItemsSource="{Binding Source={StaticResource xmlNodeList}, XPath=Node}" />
<TextBlock />
<Image /></StackPanel>
And here is an XML data:
<Root>
<Node Name="AAA" Image="images/1.ico" />
<Node Name="BBB" Image="images/2.ico">
<ChildNode Name="bbb 1" Image="images/3.ico">
<GrandchildNode Name="b 1.1" Image="images/4.ico"/>
<GrandchildNode Name="b 1.2" Image="images/5.ico"/>
<GrandchildNode Name="b 1.3" Image="images/6.ico"/>
</ChildNode>
<ChildNode Name="bbb 2" Image="images/7.ico"/>
<ChildNode Name="bbb 3" Image="images/8.ico">
<GrandchildNode Name="b 3.1" Image="images/9.ico"/>
<GrandchildNode Name="b 3.2" Image="images/10.ico"/>
</ChildNode>
<ChildNode Name="bbb 4" Image="images/11.ico"/>
</Node>
<Node Name="CCC" Image="images/12.ico">
<ChildNode Name="ccc 1" Image="images/13.ico">
<GrandchildNode Name="c 1.1" Image="images/14.ico"/>
<GrandchildNode Name="c 2.2" Image="images/15.ico"/>
</ChildNode>
</Node></Root>
If you stick you TextBlock & Image inside another StackPanel to make it a bit easier you can do:
<StackPanel DataContext="{Binding ElementName=treeView1, Path=SelectedItem}">
<TextBlock Text="{Binding XPath=#Name}" />
<Image Source="{Binding XPath=#Image}" />
</StackPanel>