<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:EPOS.Desktop.UserControls"
xmlns:dxlc="http://schemas.devexpress.com/winfx/2008/xaml/layoutcontrol"
xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls" xmlns:dxnav="http://schemas.devexpress.com/winfx/2008/xaml/navigation"
x:Class="EPOS.Desktop.UserControls.QeueOrders" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="600"
xmlns:tiles="clr-namespace:EPOS.Desktop.ViewModel">
<UserControl.DataContext>
<tiles:DemoViewModel x:Name="xyz"/>
</UserControl.DataContext>
<UserControl.Resources>
<StackPanel x:Key="tileTemplate">
<ListView>
<ListView.ItemTemplate>
<dxlc:Tile Content="{Binding Name}"/>
</ListView.ItemTemplate>
</ListView>
</StackPanel>
</UserControl.Resources>
<Grid>
<dxlc:TileLayoutControl ItemsSource="{Binding demos}" ItemTemplate="{StaticResource tileTemplate}" Grid.RowSpan="2"
Margin="21,0,-21,0"/> <!-- <dxlc:Tile Header="{Binding Name}" Click="Tile_Click" /> -->
<TextBox x:Name="textBox" HorizontalAlignment="Left" Height="23" Margin="144,10,0,0" TextWrapping="Wrap" Text="TextBox"
VerticalAlignment="Top" Width="120"/>
</Grid>
</UserControl>
I want search tiles in WPF. So far I have found that it's possible only if I use listview. If you know any other best possible way, please share the solution.
this is xml:
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:EPOS.Desktop.UserControls"
xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"
xmlns:dxlc="http://schemas.devexpress.com/winfx/2008/xaml/layoutcontrol" xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls" xmlns:dxnav="http://schemas.devexpress.com/winfx/2008/xaml/navigation" x:Class="EPOS.Desktop.UserControls.QeueOrders"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="600"
xmlns:tiles="clr-namespace:EPOS.Desktop.ViewModel"
>
<UserControl.DataContext>
<tiles:DemoViewModel x:Name="xyz"/>
</UserControl.DataContext>
<Grid>
<!--<dxlc:TileLayoutControl ItemsSource="{Binding demos}" ItemTemplate="{StaticResource tileTemplate}" Grid.RowSpan="2" Margin="21,0,-21,0" />-->
<!-- <dxlc:Tile Header="{Binding Name}" Click="Tile_Click" /> -->
<dxlc:TileLayoutControl Name="tileLayoutControl" ItemsSource="{Binding demos}" >
<dxlc:TileLayoutControl.ItemTemplate>
<DataTemplate>
<dxlc:Tile Header="{Binding Name}" Content="{Binding Name}"/>
</DataTemplate>
</dxlc:TileLayoutControl.ItemTemplate>
</dxlc:TileLayoutControl>
<Grid>
<dxe:SearchControl Name="search" Height="30" Width="200" Margin="38,0,362,270"/>
</Grid>
</Grid>
</UserControl>
This is with code-behind:
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;
using EPOS.Desktop.ViewModel;
using EPOS.Desktop.View;
using EPOS.Desktop.Model;
using System.ComponentModel;
using DevExpress.Xpf.Editors;
using DevExpress.Xpf.LayoutControl;
namespace EPOS.Desktop.UserControls
{
/// <summary>
/// Interaction logic for QeueOrders.xaml
/// </summary>
public partial class QeueOrders : UserControl
{
public QeueOrders()
{
InitializeComponent();
DependencyPropertyDescriptor dpd = DependencyPropertyDescriptor.FromProperty(SearchControl.SearchTextProperty, typeof(SearchControl));
if (dpd != null)
{
dpd.AddValueChanged(search, OnSearchTextChanged);
}
}
private void OnSearchTextChanged(object sender, EventArgs e)
{
SearchControl tb = sender as SearchControl;
foreach (Tile t in tileLayoutControl.GetLogicalChildren(false))
{
if ((String.IsNullOrEmpty(tb.SearchText)) || (t.Content != null && t.Content.ToString().Contains(tb.SearchText)))
t.Visibility = Visibility.Visible;
else
t.Visibility = Visibility.Collapsed;
}
}
DemoViewModel demovm = new DemoViewModel();
private void Tile_Click(object sender, EventArgs e)
{
CategoryForm catform = new CategoryForm();
catform.ShowDialog();
}
}
}
But i want to do using MVVM patttern.
Related
I have created a Usercontrol that I would like to make resizable using the thumb control. I have added my control to a Canvas and when I click on the thumb control the only events that are fired are Dragend and DragStart (in that order) and DragDelta is not even firing. I have found someone with a similar problem Thumb inside an ItemsControl not firing DragDelta event but there is no way I can ask them if they found a solution.
This is the code :
<UserControl x:Class="test.ucChair"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="53" d:DesignWidth="93" RenderTransformOrigin="0.5,0.5" Background="Beige"
>
<Grid Name="grd" Margin="0,0,0,0">
<Rectangle x:Name="rectAngle" Margin="0,0,0,0" Stroke="Black" RenderTransformOrigin="0.5,0.5" Grid.ZIndex="1000" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" MinHeight="53" /> <!-- Width="93" Height="53"-->
<TextBox x:Name="txtName" Text="OPEN" Margin="0,0,0,0" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" IsReadOnly="True" IsReadOnlyCaretVisible="False" TextWrapping="Wrap" Width="93" KeyDown="txtName_KeyDown" FlowDirection="RightToLeft" BorderThickness="0" FontSize="14" Cursor="Hand" TextAlignment="Center"/>
<Thumb Name="thmbResize" DockPanel.Dock="Right" VerticalAlignment="Bottom" Height="15" Width="15"
DragDelta="OnResizeThumbDragDelta"
DragStarted="OnResizeThumbDragStarted"
DragCompleted="OnResizeThumbDragCompleted" HorizontalAlignment="Right">
</Thumb>
</Grid>
</UserControl>
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 test
{
/// <summary>
/// Interaction logic for ucChairLeft.xaml
/// </summary>
public partial class ucChair : UserControl
{
public ChairType AngleType;
//?? public int SectionID;
public ucChair()//ChairType CType)//?, int _SectionID)
{
InitializeComponent();
}
private void OnResizeThumbDragCompleted(object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e)
{
System.Diagnostics.Debug.WriteLine("DragEnd");
Mouse.Capture(null);
}
private void OnResizeThumbDragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
{
System.Diagnostics.Debug.WriteLine("DragDelta");
}
private void OnResizeThumbDragStarted(object sender, System.Windows.Controls.Primitives.DragStartedEventArgs e)
{
Mouse.Capture(this);
System.Diagnostics.Debug.WriteLine("DragStart");
}
}
}
<Window xmlns:test="clr-namespace:test" x:Class="test.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="300" Width="300">
<Canvas Width="300" Height="300" Background="LightBlue">
<test:ucChair Canvas.Left="50" Canvas.Top="100" Width="50" Height="50" Background="Yellow"/>
</Canvas>
</Window>
Any help greatly appreciated!
This is my first question on stackoverflow.
I have a Usercontrol that contains a viewModel as a staticresource. Than I have a Window that contains this UserModel. I want to send commands from the Window to the viewmodel that is used by the usercontrol. So I bound the Viewmodel class to the Usercontrol and to the Window as a static Resource. But the viewmodell ist instanciated 2 times. Once from the Window and once from the usercontrol. What am I doing wrong?
Here is the xaml of the usercontrol:
<UserControl x:Class="ScanVM.ScanView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:t="clr-namespace:ScanVM"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="501.305">
<UserControl.Resources>
<t:ScanVM x:Key="ABC"/>
</UserControl.Resources>
<DockPanel DataContext="{Binding Source={StaticResource ABC}}">
<Grid ShowGridLines="True" Margin="10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="5*" />
<ColumnDefinition Width="1*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="1*" />
<RowDefinition Height="10*" />
<RowDefinition Height="1*" />
</Grid.RowDefinitions>
<Label Content="Geräte:" Grid.Column="0" Grid.Row="1" HorizontalAlignment="Right" Margin="0,10,10,0"/>
<DataGrid ItemsSource="{Binding Path=ScanIDs, Mode=OneWay}" Grid.Column="1" HorizontalAlignment="Left" Margin="10,10,0,0" Grid.Row="1" VerticalAlignment="Top" AutoGenerateColumns="false" Height="152" >
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding ScanIds}" ClipboardContentBinding="{x:Null}" Header="Bezeichnung" MinWidth="200"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</DockPanel>
This is the code of my window:
<Window x:Class="ScanViewer.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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ribbon="clr-namespace:System.Windows.Controls.Ribbon;assembly=System.Windows.Controls.Ribbon"
xmlns:self="clr-namespace:ScanViewer"
xmlns:t="clr-namespace:ScanVM;assembly=ScanVM"
mc:Ignorable="d"
Title="Scan-Viewer" Height="682.225" Width="694">
<Window.Resources>
<t:ScanVM x:Key="ABC"/>
</Window.Resources>
<Window.CommandBindings>
<CommandBinding
Command="self:MainWindow.ExitCommand"
CanExecute="ExitCommand_CanExecute"
Executed="ExitCommand_Execute"/>
</Window.CommandBindings>
<DockPanel>
<ribbon:Ribbon DockPanel.Dock="Top">
<Ribbon.ApplicationMenu>
<RibbonApplicationMenu Visibility="Collapsed">
<RibbonApplicationMenuItem Header="Beenden"/>
<RibbonApplicationMenu.FooterPaneContent>
<RibbonButton Label="Exit"/>
</RibbonApplicationMenu.FooterPaneContent>
</RibbonApplicationMenu>
</Ribbon.ApplicationMenu>
<RibbonTab Header="Start">
<RibbonGroup Header="Programm">
<RibbonButton Label="Beenden" Margin="0,5,0,0" Command="self:MainWindow.ExitCommand" />
</RibbonGroup>
<RibbonGroup Header="Scannen">
<RibbonButton Label="Daten Scannen" Margin="0,5,0,0" Command="{Binding ScanCmd, Source={StaticResource ABC}}"/>
</RibbonGroup>
</RibbonTab>
</ribbon:Ribbon>
<Grid DockPanel.Dock="Top" Height="464">
<t:ScanView DataContext="{Binding Source={StaticResource ABC}}"/>
</Grid>
</DockPanel>
And this is the code of my Viewmodel:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Windows.Input;
using System.Threading;
using MVVMBase;
namespace ScanVM
{
public class ScanVM : ViewModelBase
{
private ICommand _doWorkCmd;
private ICommand _scanCmd;
private bool scanAllowed = false;
public List<int> ScanIDs
{
get;
set;
}
public ScanVM()
{
_doWorkCmd = new DelegateCommand((param) => DoWork());
_scanCmd = new DelegateCommand((param) => Scan());
}
public bool NWScanAllowed(object executeAllowed)
{
return scanAllowed;
}
public ICommand WorkCmd
{
get { return _doWorkCmd; }
}
public ICommand ScanCmd
{
get
{
return _scanCmd;
}
}
private void Scan()
{
scanAllowed = true;
}
private void DoWork()
{
}
}
}
The answer depends on what you want to do with the shared object.
You can either move ABC to App.xaml inside <Application.Resources> tag to share it throughout your project.
Or you can remove the one in user control and use the ABC in window's resources.
You will need a valid reference path to address the ABC in window resource from user control in this case.
which brings us to these two situations:
The valid path is possible to find:
So let's say if your user control is a part of the mentioned window (and if the window's DataContext is set to ABC) then inside the user control you will reference it like this:
<DockPanel DataContext="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=DataContext}">
The valid path is impossible to find and you just want the static resource. If you insist on doing it this way it is better to just move the resource to App.xaml than to try to tweak this out.
This question is related to removing an attribute in xaml.
In the below code, I have a Span. During an event, I add a back ground to this Span. During another event, I need to remove it. Please let me know, if there is a way to remove the background attribute I set to Span.
My Xaml looks like this.
<Window x:Class="WpfApplication4.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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication4"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition Height="40"></RowDefinition>
</Grid.RowDefinitions>
<RichTextBox x:Name="rtb">
<FlowDocument>
<Paragraph>
<Span x:Name="def" Tag="default">
<Run x:Name="deg">Some Text</Run>
</Span>
</Paragraph>
</FlowDocument>
</RichTextBox>
<TextBox x:Name="tx" Grid.Row="1" TextWrapping="Wrap"/>
<Grid Grid.Row="2">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Button Content="s" Click="Button_Click"/>
<Button Content="bg-Add" Grid.Column="1" Click="Button_Click"/>
<Button Content="bg-Remove" Grid.Column="2" Click="Button_Click"/>
</Grid>
</Grid>
</Window>
My code looks like this.
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.Markup;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows. Shapes;
namespace WpfApplication4
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if ((sender as Button).Content.ToString() == "s")
{
tx.Text = XamlWriter.Save(def);
}
else if ((sender as Button).Content.ToString() == "bg-Add")
{
def.Background = new SolidColorBrush(Colors.Blue);
}
else if ((sender as Button).Content.ToString() == "bg-Remove")
{
//Need to remove the set back color so that I get the default back
}
}
}
}
I am able to find an answer myself.
def.ClearValue(Span.BackgroundProperty);
is going to remove that attribute for me.
You can override ShouldSerializeProperty
public class CustomizedSpan : Span
{
public bool IsRemoveBackGround;
protected override bool ShouldSerializeProperty(DependencyProperty dp)
{
if (dp == Span.BackgroundProperty && IsRemoveBackGround)
{
return false;
}
else
{
return base.ShouldSerializeProperty(dp);
}
}
}
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 set up LINQ-to-SQL / NorthWind in WPF.
The ListBox shows data but the DataGrid doesn't (no errors, just doesn't display anything).
I referenced WPFToolkit.dll.
Why is the DataGrid not displaying the data that ListBox can?
XAML:
<Window x:Class="TestLinq343.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dg="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<DataTemplate x:Key="ShowCustomer">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding CategoryID}"/>
<TextBlock Text=": "/>
<TextBlock Text="{Binding ProductName}"/>
</StackPanel>
</DataTemplate>
</Window.Resources>
<Grid>
<dg:DataGrid x:Name="TheDataGrid" AutoGenerateColumns="True"></dg:DataGrid>
<ListBox x:Name="TheListBox" ItemTemplate="{StaticResource ShowCustomer}"/>
</Grid>
</Window>
code behind:
using System.Linq;
using System.Windows;
using TestLinq343.Model;
using Microsoft.Windows.Controls;
namespace TestLinq343
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
NorthwindDataContext db = new NorthwindDataContext();
var sortedProducts =
from p in db.Products
orderby p.UnitsInStock descending
select p;
TheDataGrid.ItemsSource = sortedProducts;
TheListBox.ItemsSource = sortedProducts;
}
}
}
It was just a XAML issue, this fixes it:
<ScrollViewer>
<StackPanel>
<dg:DataGrid x:Name="TheDataGrid"/>
<ListView x:Name="TheListView" ItemTemplate="{StaticResource ShowCustomer}"/>
</StackPanel>
</ScrollViewer>
maybe because you did not specifiy datagrid columns. try setting the datagrids AutoGenerateColumns property to true.