i have a problem in WPF application with binding data in ListBox.
Here is me xaml code:
<Window x:Class="DatabaseBoozeWpf.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:DatabaseBoozeWpf"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="625">
<Grid>
<ListBox
Margin="10,124,0,10"
ItemsSource="{Binding Boozes}"
HorizontalAlignment="Left"
ScrollViewer.VerticalScrollBarVisibility="Visible"
ItemTemplate="{Binding Boozes}"
Width="233">
</ListBox>
</Grid>
But if I open the program, it will show this kind on text. It should output the list of products.
You should have an ItemTemplate with a DataTemplate with elements that bind to the properties of the item class.
<ListBox ItemsSource="{Binding Boozes}" ...>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding YourProperty}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Related
I have a very simple usercontrol:
<UserControl x:Class="PointOfSale.UserControls.HousesGrid"
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="300" d:DesignWidth="300">
<ItemsControl x:Name="LayoutRoot" ItemsSource ="{Binding PopularHouses}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="5"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<ToggleButton
Content="{Binding FormattedPanelTimeRemaining}"
Style="{StaticResource MetroToggleButtonStyle}"
Height="45"
Width="80"
VerticalAlignment="Center"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
As you can see the ItemSource property is bound to the PopularHouses property on the ViewModel of the parent. This works great. However, what I want to do is set the ItemSource of the LayoutRoot element to a different property at the point on the parent form where the control is inserted in the XAML.
The end result should then be multiple instances of this user control, bound to several different properties on the parent's datacontext.
Could someone explain how to implement this?
You just have to bind your UserControl's DataContext to the datacontext of the first ContentControl using RelativeSource.
DataContext="{Binding Path=DataContext, RelativeSource={RelativeSource AncestorType={x:Type ContentControl}}}"
I have made the following sample:
The mainwindow XAML
<Window x:Class="WpfDataContext.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfDataContext"
Title="MainWindow" Height="350" Width="525"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Grid>
<local:UserControl1/>
</Grid>
</Window>
We set its datacontext to Self just for the purpose of this sample. In codebehind we define a simple property to show how it works:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public string SomeString { get; set; } = "Hello";
}
Then, the usercontrol XAML:
<UserControl x:Class="WpfDataContext.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
DataContext="{Binding Path=DataContext, RelativeSource={RelativeSource AncestorType={x:Type ContentControl}}}">
<Grid>
<TextBlock Text="{Binding SomeString}"/>
</Grid>
</UserControl>
Note how we bind its DataContext property since this is the key.
I use a Textblock for simplicity, but the principle applies for your case also
I am using the Xceed Extended WPF Toolkit for the Integer Up Down Control. I have installed this via Nuget.
I have the integrated into a custom control which contains other normal textboxes and button etc.
This custom control I then put in a tab control on a Window. Everything shows correctly apart from this IntegerUpDown which shows as an empty box. (It is fine when looking at the custom control in design)
I have added the namespace to both the control and window so am not sure what the problem is. Everything is inside one project so I don't think references are a problem.
Any ideas of what I could be missing?
Control XAML:
<Label Grid.Row="3" Content="Quantity of Tickets:" VerticalAlignment="Center"></Label>
<xctk:IntegerUpDown Grid.Row="3" Grid.Column="1" Name="numTickets"></xctk:IntegerUpDown>
Form XAML:
<TabItem Header="New Booking">
<Grid Background="#FFE5E5E5">
<btc:NewBooking></btc:NewBooking>
</Grid>
</TabItem>
Thanks,
Ryan
Here's a MCVE:
MainWindow:
<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:WpfApplication8"
x:Class="WpfApplication8.MainWindow"
mc:Ignorable="d"
Title="MainWindow" Height="300" Width="300">
<Grid>
<TabControl x:Name="tabControl" Margin="0">
<TabItem Header="TabItem 1">
<Grid Background="#FFE5E5E5">
<local:UserControl1></local:UserControl1>
</Grid>
</TabItem>
<TabItem Header="TabItem 2">
<Grid Background="#FFE5E5E5"/>
</TabItem>
</TabControl>
</Grid>
UserControl:
<UserControl x:Class="WpfApplication8.UserControl1"
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:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
xmlns:local="clr-namespace:WpfApplication8"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<StackPanel>
<Label Grid.Row="3" Content="Quantity of Tickets:" VerticalAlignment="Center"></Label>
<xctk:IntegerUpDown Grid.Row="3" Grid.Column="1" Name="numTickets"></xctk:IntegerUpDown>
</StackPanel>
</Grid>
Both MainWindow and UserControl are at the same level in the project structure:
EDIT: One more thing, make sure your UserControl is fully contained by the MainWindow and you do NOT have something like this:
That would cause the IntegerUpDown look like an empty box, like you described.
Using a WPF ListBox and a WrapPanel, is there a way to use a ViewBox with the internal content scaling, but without having any content trimming or scroll bars? I want to scale all internal items down. The width / heigth of the container can scale, and the number of items is dynamic.
I put together a simple WPF example (without any solutions). Open to sugguestions.
Code Example:
<Window x:Class="WpfListBoxWrapPanel.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"
mc:Ignorable="d"
Title="MainWindow"
Height="230.572"
Width="426.972">
<Grid>
<ListBox ItemsSource="{Binding SomeCollection, FallbackValue=123456123456123456123456123456123456}"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ScrollViewer.VerticalScrollBarVisibility="Disabled">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Width="50"
Height="50"
Margin="3">
<Rectangle Fill="LightCoral"></Rectangle>
<TextBlock Text="{Binding}"
Margin="4"></TextBlock>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
</Grid>
Thanks!
I have one user control in my WPF app
<UserControl x:Class="NewWPFApp.ProgressControl"
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="300" d:DesignWidth="300">
<Grid>
<Expander Header="{Binding Path=Headerval}">
<StackPanel Margin="10,4,0,0">
<DataGrid
x:Name="dataGrid"
AutoGenerateColumns="False"
ItemsSource="{Binding Path=records}"/>
</StackPanel>
</Expander>
</Grid>
and in my Mainwindow when I am doing this
<Window xmlns:NewWPFApp="clr-namespace:NewWPFApp" x:Class="NewWPFApp.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>
<ListBox x:Name="peopleListBox" >
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Background="AliceBlue">
<NewWPFApp:ProgressControl Height="100" Width="100"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
I cant see the output.
If I remove it from the Data template then it works.But not inside the data template.
What am I missing ???
Thanks
ListBox.ItemTemplate specifies how items added to a list box will be displayed. In your case, you uses your own custom control. However, at the same time you added no items to the list box so there is nothing to display. To populate the list box you can use binding e.g.:
<ListBox ItemsSource="{Binding ItemsToBeDisplayed}" x:Name="peopleListBox" >
...
</ListBox>
Where ItemsToBeDisplayed is a property of a view model (if you use MVVM pattern) that returns a collection of objects. If not you can populate ItemsSource without using binding:
var list = new List<People>();
//Add objects to a list
peopleListBox.ItemsSource = list;
I assumed that you have People class that models people you want to display in your application. People class should have Headerval and records properties because you use them in your your ProgressControl control. If you do as described, a new instance of your ProgressControl control will be created for every object in ItemsSource.
I am using infragistics wpf tabcontrol controls. Look at the following code snippet.
XAML
<Window x:Class="DynamicTabControl.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:igWindows="http://infragistics.com/Windows"
xmlns:igDP="http://infragistics.com/DataPresenter"
Title="MainWindow" Height="350" Width="525">
<Grid>
<igWindows:XamTabControl Name="xamTabCtrl" ItemsSource="{Binding Collection}" Theme="Metro">
<igWindows:XamTabControl.ItemTemplate>
<DataTemplate >
<Label Content="{Binding Header}" />
</DataTemplate>
</igWindows:XamTabControl.ItemTemplate>
<igWindows:XamTabControl.ContentTemplate>
<DataTemplate>
<igDP:XamDataGrid Theme="Metro"
DataSource="{Binding Logins}"/>
</DataTemplate>
</igWindows:XamTabControl.ContentTemplate>
</igWindows:XamTabControl>
</Grid>
</Window>
The datagrid looks like
As you can see the xaml above, I overwrite the ItemTemplate property wit label, that cause different appearance from infragistics default style and the default looks like
How can I inherit ItemTemplate style from infragistics?
Update
I try following:
<Window x:Class="DynamicTabControl.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:igWindows="http://infragistics.com/Windows"
xmlns:igDP="http://infragistics.com/DataPresenter"
Title="MainWindow" Height="450" Width="700">
<Grid>
<igWindows:XamTabControl Name="xamTabCtrl" ItemsSource="{Binding Collection}" Theme="Metro">
<igWindows:XamTabControl.ItemContainerStyle>
<Style TargetType="{x:Type igWindows:TabItemEx}" BasedOn="">
<Setter Property="Header" Value="{Binding Path=Header}" />
</Style>
</igWindows:XamTabControl.ItemContainerStyle>
<igWindows:XamTabControl.ContentTemplate>
<DataTemplate>
<igDP:XamDataGrid Theme="Metro"
DataSource="{Binding Logins}" IsGroupByAreaExpanded="False" GroupByAreaLocation="None" GroupByAreaMode="DefaultFieldLayoutOnly"/>
</DataTemplate>
</igWindows:XamTabControl.ContentTemplate>
</igWindows:XamTabControl>
</Grid>
</Window>
Everything works fine, except the theme does not take it with. What do I have to write in baseOn property?