I am developing a wpf app. I have a database like this.
I have three columns(id,name,profession).listbox shows name column.
When the user clicks the item in listbox, i wanna show his/her profession
in textblock.
listbox works well. I have bounded it to a dataview. but how can i show his/her profession in textblock?
private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
//What can I write here?
}
You should proably just bind the text attribute of the TextBlock to the selected item of the list box. Check this article at MSDN.
<StackPanel>
<TextBlock Width="248" Height="24" Text="Colors:"
TextWrapping="Wrap"/>
<ListBox x:Name="lbColor" Width="248" Height="56">
<ListBoxItem Content="Blue"/>
<ListBoxItem Content="Green"/>
<ListBoxItem Content="Yellow"/>
<ListBoxItem Content="Red"/>
<ListBoxItem Content="Purple"/>
<ListBoxItem Content="Orange"/>
</ListBox>
<TextBlock Width="248" Height="24" Text="You selected color:" />
<TextBlock Width="248" Height="24">
<TextBlock.Text>
<Binding ElementName="lbColor" Path="SelectedItem.Content"/>
</TextBlock.Text>
</TextBlock>
</StackPanel>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using System.Data.OleDb;
namespace WpfApplication2
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public DataView view;
public DataSet ds;
public Window1()
{
InitializeComponent();
BindData();
}
public void BindData() {
OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\\Information.mdb;Persist Security Info=True");
con.Open();
string sql = "Select * from Dictionary";
OleDbCommand cmd = new OleDbCommand(sql, con);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataSet ds = new DataSet();
try
{
da.Fill(ds, "Info");
view = ds.Tables[0].DefaultView;
listBox1.ItemsSource = view;
}
catch (Exception ex)
{
}}
private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
{
view.RowFilter = string.Format("Name Like '{0}%'", textBox1.Text);
}
}
}
<Window x:Class="WpfApplication2.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="490">
<Grid>
<ListBox Margin="18,77,0,83" Name="listBox1" ItemsSource="{Binding Path=Name}" HorizontalAlignment="Left" Width="213" SelectionChanged="listBox1_SelectionChanged" >
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Name}"> </TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<TextBox Height="23" Margin="18,40,0,0" Name="textBox1" VerticalAlignment="Top" TextChanged="textBox1_TextChanged" HorizontalAlignment="Left" Width="213" />
<TextBlock HorizontalAlignment="Right" Margin="0,77,42,83" Name="textBlock1" Width="185" Background="#FFE6C6C6" /><Label Height="28" HorizontalAlignment="Right" Margin="0,39,76,0" Name="label1" VerticalAlignment="Top" Width="140">Label</Label>
</Grid>
</Window>
Related
I've written a code for a simple calculator in wpf .
My MainWindow.Xaml code is..
<Window x:Class="CalculatorNew.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid ContextMenuClosing="Multi">
<TextBox x:Name="TextBox1" HorizontalAlignment="Left" Height="23" Margin="216,23,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" RenderTransformOrigin="0.679,0.552"/>
<TextBox x:Name="TextBox2" HorizontalAlignment="Left" Height="23" Margin="216,76,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" RenderTransformOrigin="0.679,0.552"/>
<TextBox x:Name="TextBox3" HorizontalAlignment="Left" Height="23" Margin="216,121,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" RenderTransformOrigin="0.679,0.552"/>
<TextBlock HorizontalAlignment="Left" Margin="68,30,0,0" TextWrapping="Wrap" Text="First Number" VerticalAlignment="Top" RenderTransformOrigin="1.137,1.259" Height="16" Width="80"/>
<TextBlock HorizontalAlignment="Left" Margin="68,128,0,0" TextWrapping="Wrap" VerticalAlignment="Top" RenderTransformOrigin="1.137,1.259" Height="16" Width="80" Text="Result"/>
<TextBlock HorizontalAlignment="Left" Margin="68,83,0,0" TextWrapping="Wrap" VerticalAlignment="Top" RenderTransformOrigin="1.137,1.259" Height="16" Width="99" Text="Second Number"/>
<Button Content="+" HorizontalAlignment="Left" Margin="73,172,0,0" VerticalAlignment="Top" Width="75" Click="Add"/>
<Button Content="-" HorizontalAlignment="Left" Margin="179,172,0,0" VerticalAlignment="Top" Width="75" Click="Sub"/>
<Button Content="*" HorizontalAlignment="Left" Margin="286,172,0,0" VerticalAlignment="Top" Width="75" Click="Multi"/>
<Button Content="/" HorizontalAlignment="Left" Margin="392,172,0,0" VerticalAlignment="Top" Width="75" Click="Div"/>
</Grid>
My MainWindowdow.Xaml.cs code is..
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace CalculatorNew
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Add(object sender, RoutedEventArgs e)
{
TextBox3.Text = (System.Convert.ToDecimal(TextBox1.Text) + System.Convert.ToDecimal(TextBox2.Text)).ToString();
}
private void Sub(object sender, RoutedEventArgs e)
{
TextBox3.Text = (System.Convert.ToDecimal(TextBox1.Text) - System.Convert.ToDecimal(TextBox2.Text)).ToString();
}
private void Div(object sender, RoutedEventArgs e)
{
TextBox3.Text = (System.Convert.ToDecimal(TextBox1.Text) / System.Convert.ToDecimal(TextBox2.Text)).ToString();
}
private void Multi(object sender, RoutedEventArgs e)
{
TextBox3.Text = (System.Convert.ToDecimal(TextBox1.Text) * System.Convert.ToDecimal(TextBox2.Text)).ToString();
}
}
}
I am new to WPF and MVVM . So I am finding it a bit difficult to write it in the MVVM format .
Can someone tell me how to rewrite it in MVVM format so that I can use it as a reference for the future?
I think your question has a high chance of being closed because it doesn't demonstrate a minimum willingness to make an effort on your part. Please read a tutorial, there are plenty on the web, and try to understand the concepts first. You need at the very least to understand what a ViewModel is, and how the View relates to it (bindings).
You will probably not get working code that does exactly what you want on this site, I for one am much more interested in explaining how things work.
Anyway, in order to get what you want you need a class that acts as the ViewModel with properties that represent the concepts that you work with. You could have decimal properties that represent the operands and something that represents the operator, and commands that represent each button in the UI. Then you need bindings that link UI elements (textboxes, buttons) to the properties in the ViewModel, and you need to set an instance of the ViewModel as the DataContext of the container for the UI elements you're interested in (be it a Window, Grid or whatever). I think that pretty much sums it up...
You can use this example as a reference. https://code.google.com/p/wpf-mvvm-calculator/downloads/list
My purpose is that: double-click the TabItem named "Tab" in the TabControl, then the TabControl will be folded, another double-click lead to unfolded.
But after I move the GridSplitter and double-click the "Tab" to be folded, it don't work correctly that the the height of the column the TabControl in isn't equal to the height of the TabControl, in other word, GridSplitter don't follow the "TabControl".
.xaml
<Window x:Class="WpfApplication7.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid x:Name="LayoutRoot">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto" MinHeight="20"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<Grid Grid.Column="1">
<Label>
Nothing
</Label>
</Grid>
</Grid>
<GridSplitter Grid.Row="1"
HorizontalAlignment="Stretch"
VerticalAlignment="Center"
Height="6" />
<TabControl Grid.Row="2"
TabStripPlacement="Bottom"
VerticalAlignment="Stretch">
<TabItem Header="Tab"
MouseDoubleClick="TabItemDoubleCilck">
<!--ListView-->
<ListView Grid.Row="1"
xmlns:sys="clr-namespace:System;assembly=mscorlib">
<ListView.View>
<GridView>
<GridViewColumn Header="AA "
Width="100"
DisplayMemberBinding="{Binding [0]}" />
<GridViewColumn Header="BB"
Width="100"
DisplayMemberBinding="{Binding [1]}" />
<GridViewColumn Header="CC"
Width="100"
DisplayMemberBinding="{Binding [2]}" />
<GridViewColumn Header="DD"
Width="100"
DisplayMemberBinding="{Binding [3]}" />
</GridView>
</ListView.View>
<ListViewItem>
<x:Array Type="sys:String" >
<sys:String>2000/01/01</sys:String>
<sys:String>2000/01/01</sys:String>
<sys:String>2000/01/01</sys:String>
<sys:String>2000/01/01</sys:String>
</x:Array>
</ListViewItem>
<ListViewItem>
<x:Array Type="sys:String" >
<sys:String>2000/01/01</sys:String>
<sys:String>2000/01/01</sys:String>
<sys:String>2000/01/01</sys:String>
<sys:String>2000/01/01</sys:String>
</x:Array>
</ListViewItem>
</ListView>
</TabItem>
</TabControl>
</Grid>
</Window>
.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication7
{
/// <summary>
/// MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
const double dBottomTabMinHeight = 20.0;
private bool isBottomTabUnfold;
public MainWindow()
{
InitializeComponent();
isBottomTabUnfold = true;
}
private void TabItemDoubleCilck(object sender, MouseButtonEventArgs e)
{
TabItem tabItm = sender as TabItem;
if (isBottomTabUnfold)
{
((FrameworkElement)tabItm.Parent).Height = dBottomTabMinHeight;
}
else
{
((FrameworkElement)tabItm.Parent).Height = Double.NaN;
}
isBottomTabUnfold = !isBottomTabUnfold;
}
}
}
try this, hope this is what you expected
private void TabItemDoubleCilck(object sender, MouseButtonEventArgs e)
{
TabItem tabItm = sender as TabItem;
if (isBottomTabUnfold)
{
((FrameworkElement)tabItm.Parent).Height = dBottomTabMinHeight;
((FrameworkElement)tabItm.Parent).VerticalAlignment = System.Windows.VerticalAlignment.Bottom;
}
else
{
((FrameworkElement)tabItm.Parent).Height = Double.NaN;
((FrameworkElement)tabItm.Parent).VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
}
isBottomTabUnfold = !isBottomTabUnfold;
}
edit:
like this then?
private void TabItemDoubleCilck(object sender, MouseButtonEventArgs e)
{
TabItem tabItm = sender as TabItem;
if (isBottomTabUnfold)
{
dUnfoldedHeight = ((FrameworkElement)tabItm.Parent).ActualHeight;
((FrameworkElement)tabItm.Parent).Height = dBottomTabMinHeight;
rowTab.Height = new GridLength(1, GridUnitType.Auto);
}
else
{
((FrameworkElement)tabItm.Parent).Height = double.NaN;
rowTab.Height = new GridLength(dUnfoldedHeight);
}
isBottomTabUnfold = !isBottomTabUnfold;
}
NB:
rowTab is the name of the 3rd rowdefinition, dUnfoldedHeight is a private member to track the height of the unfolded tab before it's folded.
you might find a weird behavior on dragging the splitter up when the tab is folded or when dragging the spliiter down until the tab is look like unfolded, to fix this you probably have to work with the DragDelta and DragCompleted event of the splitter to decide whether in the end the tab should be considered as folded or not
i have application with two Tabs. on first tab placed button which sets the current position in dataGrid1 on second tab. While i won't show a second tab, i can't set current position by button1.
<UserControl x:Class="SilverlightApplication9.MainPage"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
<Grid x:Name="LayoutRoot" Background="White">
<sdk:TabControl Height="234" HorizontalAlignment="Left" Margin="52,44,0,0" Name="tabControl1" VerticalAlignment="Top" Width="326">
<sdk:TabItem Header="tabItem1" Name="tabItem1">
<Grid>
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="74,44,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
</Grid>
</sdk:TabItem>
<sdk:TabItem Header="tabItem2" Name="tabItem2">
<Grid>
<sdk:DataGrid ItemsSource="{Binding strs}" RowBackground="White" AutoGenerateColumns="False" Height="141" HorizontalAlignment="Left" Margin="36,12,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="199">
<sdk:DataGrid.Columns>
<sdk:DataGridTextColumn Binding="{Binding}" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Width="Auto" />
</sdk:DataGrid.Columns>
</sdk:DataGrid>
</Grid>
</sdk:TabItem>
</sdk:TabControl>
</Grid>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
namespace SilverlightApplication9
{
public partial class MainPage : UserControl
{
private ObservableCollection<string> _strs = new ObservableCollection<string>();
public ObservableCollection<string> strs { get { return _strs; } set { _strs = value; } }
public MainPage()
{
this.DataContext = this;
InitializeComponent();
strs.Add("1");
strs.Add("2");
strs.Add("3");
strs.Add("4");
strs.Add("5");
}
private void button1_Click(object sender, RoutedEventArgs e)
{
dataGrid1.SelectedIndex = 2;
}
}
}
The problem is that the DataGrid isn't loaded when you attempt to set the SelectedIndex in the button click handler if you haven't already navigated to the tab that contains the DataGrid.
The way to achieve what you want is to use data binding. You will also want to implement INotifyPropertyChanged for any subsequent changes to the property you bind DataGrid.SelectedIndex to. The following is a rough example of how to do what you want in the code you provided.
public partial class MainPage : UserControl, INotifyPropertyChanged
{
private ObservableCollection<string> _strs
= new ObservableCollection<string>();
public ObservableCollection<string> strs
{
get { return _strs; }
set { _strs = value; }
}
public MainPage()
{
this.DataContext = this;
InitializeComponent();
strs.Add("1");
strs.Add("2");
strs.Add("3");
strs.Add("4");
strs.Add("5");
SelectedIndex = 0;
}
private int _selectedIndex;
public int SelectedIndex
{
get { return _selectedIndex; }
set
{
_selectedIndex = value;
var pChanged = PropertyChanged;
if (pChanged != null)
pChanged(this, new PropertyChangedEventArgs("SelectedIndex"));
}
}
private void button1_Click(object sender, RoutedEventArgs e)
{
SelectedIndex ++;
}
public event PropertyChangedEventHandler PropertyChanged;
}
Then update your DataGrid definition in xaml to:
<sdk:DataGrid ItemsSource="{Binding strs}"
SelectedIndex="{Binding SelectedIndex, Mode=TwoWay}"
RowBackground="White"
AutoGenerateColumns="False"
Height="141"
HorizontalAlignment="Left"
Margin="36,12,0,0"
Name="dataGrid1"
VerticalAlignment="Top"
Width="199">
<sdk:DataGrid.Columns>
<sdk:DataGridTextColumn Binding="{Binding}"
CanUserReorder="True"
CanUserResize="True"
CanUserSort="True"
Width="Auto" />
</sdk:DataGrid.Columns>
</sdk:DataGrid>
In the following XAML UserControl I am binding a few items to properties in the UserControl's linked class.
<UserControl x:Class="Kiosk.EventSelectButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Kiosk"
Height="130" Width="130">
<Grid>
<Button
Style="{DynamicResource DarkButton130x130}"
HorizontalAlignment="Left"
VerticalAlignment="Center">
<Grid Margin="0,0,0,0" Height="118" Width="118">
<Image VerticalAlignment="Top" HorizontalAlignment="Center" Source="image/select_button_arrows.png" />
<Image x:Name="EventImageComponent" VerticalAlignment="Center" HorizontalAlignment="Center" Effect="{DynamicResource KioskStandardDropShadow}" Source="{Binding Path=EventImage}" />
<TextBlock x:Name="SelectTextBlock" Text="{Binding Path=SelectText}" VerticalAlignment="Top" HorizontalAlignment="Center" Margin="0,-2,0,0" FontSize="10pt" Foreground="#5aaff5" />
<TextBlock x:Name="LabelTextBlock" VerticalAlignment="Bottom" HorizontalAlignment="Right" Margin="0,0,0,0" FontSize="14pt" FontWeight="Bold" Text="{Binding Path=Label}"/>
</Grid>
</Button>
</Grid>
</UserControl>
In the linked class' contstructor I'm applying the DataContext of the items to this, as you can see below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Kiosk
{
/// <summary>
/// Interaction logic for EventSelectButton.xaml
/// </summary>
public partial class EventSelectButton : UserControl
{
public String ValueContainer;
private String _EventImage;
public String EventImage
{
get
{
return _EventImage;
}
set
{
_EventImage = value;
}
}
private String _Label;
public String Label
{
get
{
return _Label;
}
set
{
_Label = value;
}
}
private String _SelectText;
public String SelectText
{
get
{
return _SelectText;
}
set
{
_SelectText = value;
}
}
public EventSelectButton()
{
InitializeComponent();
LabelTextBlock.DataContext = this;
SelectTextBlock.DataContext = this;
EventImageComponent.DataContext = this;
}
}
}
Edit
Although this works as intended, I'm interested to know if there is a simpler way of doing this. (edit, lessons learned.) This won't actually work beyond the initialisation, the public properties will be set, however because the class doesn't use DependentProperties or alternatively, implement INotifyPropertyChanged, binding will not work as expected. (end edit)
For example,
Can I set the DataContext of these items in the XAML to this (as the EventSelectButton instance), and if so, how?
Alternatively, is it possible to inherit the DataContext from the UserControl parent, thus making the Binding Paths simpler.
The only alternatives I've found so far are more verbose, e.g. using the RelativeSource binding method to locate the EventSelectButton Ancestor.
So please, let me know any ways I can improve this binding expression, and any comments on best practices for binding within a UserComponent are much appreciated.
One way is to do the following:
Name your UserControl in your XAML.
Bind the DataContext of the root element (i.e. Grid) to the UserControl.
Like this:
<UserControl x:Name="uc">
<Grid DataContext="{Binding ElementName=uc}">
.
.
.
</Grid>
</UserControl>
Now, you'll ask, why not just set the DataContext of the UserControl itself? Well, this just ensures that setting the DataContext of an instance of the UserControl will still work without affecting the bindings in the UserControl's visual tree. So something like the one below will still work fine.
<Window>
<uc:EventSelectButton DataContext="{Binding SomeDataContext}" Width="{Binding SomeDataContextWidth}"/>
</Window>
EDIT
To make the solution complete requires the properties in the UserControl to be changed to use DependencyProperty objects instead. Below are the updates to the codes:
XAML:
<UserControl x:Class="Kiosk.EventSelectButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Kiosk"
x:Name="root"
Height="130" Width="130">
<Grid x:Name="LayoutRoot" DataContext="{Binding ElementName=root}">
<Button
Style="{DynamicResource DarkButton130x130}"
HorizontalAlignment="Left"
VerticalAlignment="Center">
<Grid Margin="0,0,0,0" Height="118" Width="118" >
<Image VerticalAlignment="Top" HorizontalAlignment="Center" Source="image/select_button_arrows.png" />
<Image Source="{Binding EventImage}" VerticalAlignment="Center" HorizontalAlignment="Center" Effect="{DynamicResource KioskStandardDropShadow}" />
<TextBlock Text="{Binding SelectText}" VerticalAlignment="Top" HorizontalAlignment="Center" Margin="0,-2,0,0" FontSize="10pt" Foreground="#5aaff5" />
<TextBlock Text="{Binding Label}" VerticalAlignment="Bottom" HorizontalAlignment="Right" Margin="0,0,0,0" FontSize="14pt" FontWeight="Bold" />
</Grid>
</Button>
</Grid>
</UserControl>
Code-Behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Kiosk
{
public partial class EventSelectButton : UserControl
{
public static readonly DependencyProperty EventImageProperty =
DependencyProperty.Register(
"EventImage",
typeof(string),
typeof(EventSelectButton));
public String EventImage
{
get { return (string)GetValue(EventImageProperty); }
set { SetValue(EventImageProperty, value); }
}
public static readonly DependencyProperty SelectTextProperty =
DependencyProperty.Register(
"SelectText",
typeof(string),
typeof(EventSelectButton));
public String SelectText
{
get { return (string)GetValue(SelectTextProperty); }
set { SetValue(SelectTextProperty, value); }
}
public static readonly DependencyProperty LabelProperty =
DependencyProperty.Register(
"Label",
typeof(string),
typeof(EventSelectButton));
public String Label
{
get { return (string)GetValue(LabelProperty); }
set { SetValue(LabelProperty, value); }
}
public EventSelectButton()
{
InitializeComponent();
}
}
}
The following example successfully binds objects with a ListBox to display them.
However, I would like to create all the objects in one class and then from another class query them with LINQ to fill my XAML ListBox, what would I need to add this example:
XAML:
<Window x:Class="WpfApplication15.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300"
xmlns:local="clr-namespace:WpfApplication15">
<Window.Resources>
<ObjectDataProvider x:Key="customers" ObjectType="{x:Type local:Customers}"/>
<DataTemplate x:Key="LastNameFirst" DataType="WpfApplication15.Customer">
<StackPanel Margin="10 10 10 0" Orientation="Horizontal">
<TextBlock Text="{Binding Path=LastName}" FontWeight="bold"/>
<TextBlock Text=", "/>
<TextBlock Text="{Binding Path=FirstName}"/>
</StackPanel>
</DataTemplate>
</Window.Resources>
<Grid>
<ListBox ItemsSource="{Binding Source={StaticResource customers}}"
ItemTemplate="{StaticResource LastNameFirst}"/>
</Grid>
</Window>
Code behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication15
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
}
public class Customer
{
public string FirstName { get; set; }
public string LastName { get; set; }
public Customer(string firstName, string lastName)
{
this.FirstName = firstName;
this.LastName = lastName;
}
}
public class Customers : List<Customer>
{
public Customers()
{
this.Add(new Customer("Jim", "Thompson"));
this.Add(new Customer("Julie", "Watson"));
this.Add(new Customer("John", "Walton"));
}
}
}
edit: added ToList call to the LINQ query
You can just assign the ItemsSource of the ListBox using LINQ in code-behind for this. Assuming you give your ListBox a name:
<Window x:Class="WpfApplication15.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:local="clr-namespace:WpfApplication15"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="300" Height="300" Title="Window1">
<Window.Resources>
<DataTemplate x:Key="LastNameFirst" DataType="WpfApplication15.Customer">
<StackPanel Margin="10 10 10 0" Orientation="Horizontal">
<TextBlock FontWeight="bold" Text="{Binding Path=LastName}"/>
<TextBlock Text=", "/>
<TextBlock Text="{Binding Path=FirstName}"/>
</StackPanel>
</DataTemplate>
</Window.Resources>
<Grid>
<ListBox x:Name="lstCustomers"
ItemTemplate="{StaticResource LastNameFirst}"/>
</Grid>
</Window>
You can assign to ItemsSource in the Loaded event:
public partial class Window1 : Window
{
public Window1()
{
this.Loaded += new RoutedEventHandler(Window1_Loaded);
InitializeComponent();
}
void Window1_Loaded(object sender, RoutedEventArgs e)
{
Customers customers = new Customers();
lstCustomers.ItemsSource = customers.Where(customer => customer.LastName.StartsWith("W")).ToList();
}
}
Assuming your LINQ query will change depending on some logic, you can re-assign ItemsSource at the appropriate points.
If you want to bind without dipping into code-behind whenever your query logic changes, you're probably better off using a CollectionViewSource, since it has sorting and filtering capabilities (assuming that's what you're after from using LINQ).
Take a look # http://www.codeplex.com/bindablelinq and you should find a bunch of good examples for this.