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>
Related
enter code here<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:EPOS.Desktop.View"
xmlns:vm="clr-namespace:EPOS.Desktop.ViewModel"
xmlns:UserControls="clr-namespace:EPOS.Desktop.UserControls"
xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core" xmlns:dxwui="http://schemas.devexpress.com/winfx/2008/xaml/windowsui" xmlns:dxn="http://schemas.devexpress.com/winfx/2008/xaml/navbar" x:Class="EPOS.Desktop.View.MainSaleUI"
mc:Ignorable="d"
Title="MainSaleUI"
Width="1046" Height="500" Left="500" Top="500"
Background="SkyBlue"
WindowStartupLocation="CenterScreen"
>
<Window.Resources>
<DataTemplate DataType="{x:Type vm:QeueOrdersViewViewModel}">
<UserControls:QeueOrders />
</DataTemplate>
<DataTemplate DataType="{x:Type vm:TillViewModel}">
<UserControls:TillUC/>
</DataTemplate>
<DataTemplate DataType="{x:Type vm:SettingViewModel}">
<UserControls:Settings/>
</DataTemplate>
</Window.Resources>
<Grid Margin="0,67,65,0">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="3*"/>
</Grid.ColumnDefinitions>
<ListBox x:Name="ListBoxMenu"
Grid.Column="0" Margin="5"
ItemsSource="{Binding Settings}"
SelectedIndex="0">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" Padding="10"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Border Grid.Column="1" Margin="5,5,10,5" BorderBrush="#FF7F9DB9" BorderThickness="1">
<ContentControl Content="{Binding ElementName=ListBoxMenu, Path=SelectedItem}" Margin="0,0,225,0"/>
</Border>
</Grid>
Blockquote
How to add images to User-control menu in Wpf. These are dynamic menus. Is there way to add images to every text?
I'm not sure about what you exactly want... assuming that you want to display some image right after /before the text on your listbox you can add an image property in your "settings" class (Binding of your Listbox's Itemsource) and change your listbox's datatemplate content with something like this :
<StackPanel>
<TextBlock Text="{Binding Name}" Padding="10"/>
<Image Source="{Binding Image }" />
</StackPanel>
I have no idea what's happening here. When I bind directly to a TextBox the value can be edited, but I want to bind in a ContentControl.
Why doesn't the ContentControl update the ViewModel?
<Window x:Class="WTFWPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WTFWPF"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Title="MainWindow"
Width="525"
Height="350"
DataContext="{DynamicResource ViewModel}"
mc:Ignorable="d">
<Window.Resources>
<local:MainViewModel x:Key="ViewModel" />
<DataTemplate DataType="{x:Type sys:Int32}">
<TextBox Text="{Binding Path=., UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>
</Window.Resources>
<StackPanel>
<TextBlock Margin="5" Text="{Binding Number}" />
<TextBox Margin="5" Text="{Binding Number, UpdateSourceTrigger=PropertyChanged}" />
<ContentControl Margin="5" Content="{Binding Number}" />
</StackPanel>
</Window>
This seem to work fine, not sure why your version doesn't work though.
<Window.Resources>
<wpfApplication1:MainViewModel x:Key="ViewModel" />
<DataTemplate x:Key="NumberTemplate">
<TextBox Text="{Binding Path=Number, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>
</Window.Resources>
<StackPanel>
<TextBlock Margin="5" Text="{Binding Number}" />
<TextBox Margin="5" Text="{Binding Number, UpdateSourceTrigger=PropertyChanged}" />
<ContentControl Margin="5"
Content="{Binding}"
ContentTemplate="{StaticResource NumberTemplate}" />
</StackPanel>
Another way of making it work is to change the binding in the template:
<TextBox Text="{Binding Path=DataContext.Number,
RelativeSource={RelativeSource AncestorType=ContentControl}, UpdateSourceTrigger=PropertyChanged}" />
Unfortunately I can't explain why your version doesn't work.
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>
One more easy one.
Resources.xaml contains:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<DataTemplate DataType="TestInstanceViewModel" x:Name="TestInstanceViewModelTemplate">
<StackPanel Orientation="Vertical">
<Button Command="{Binding Path=StartCommand}" Content="Start"/>
<Button Command="{Binding Path=StopCommand}" Content="Stop"/>
<TextBlock Text="{Binding Path=Status}"/>
</StackPanel>
</DataTemplate>
Window contains:
<Window x:Class="TestClientMainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Testing client" Height="350" Width="525"
DataContext="{StaticResource ResourceKey=TheViewModel}" Background="#FFD4BFBF">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
<StackPanel HorizontalAlignment="Stretch" Name="stackPanel1" VerticalAlignment="Stretch">
<ToolBar Height="26" Name="toolBar1">
<ItemsControl>
<Button Command="{Binding Path=CreateNewTestCommand}">Add new Test</Button>
</ItemsControl>
</ToolBar>
<ListBox ItemsSource="{Binding Path=TestInstances}" ItemTemplate="{StaticResource TestInstanceViewModelTemplate}" Name="listBox1" VerticalAlignment="Stretch" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" MinHeight="{Binding ElementName=stackPanel1, Path=Height}" Height="274" />
</StackPanel>
</Grid>
Then there's a list box where I try:
ItemTemplate="{StaticResource TestInstanceViewModelTemplate}"
This doesn't work. What's the logic behind accessing the resource which I've added to the merged dictionaries?
Thanks
Edited:
Try <DataTemplate DataType="TestInstanceViewModel" x:Key="TestInstanceViewModelTemplate">
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.