why i couldn't see any data in WPF Grid? - c#

<DataTemplate x:Key="tmpGrdProducts">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Image Grid.RowSpan="6" Width="200" Height="200" Grid.Column="0" Grid.Row="0"></Image>
<TextBlock Grid.ColumnSpan="2" Grid.Row="0" Grid.Column="1" Text="{Binding ProductName}" Foreground="Red"></TextBlock>
<!-- Labels-->
<TextBlock Text="SKU" Foreground="Red" Grid.Column="1" Grid.Row="1"></TextBlock>
<TextBlock Text="Code" Foreground="Red" Grid.Column="1" Grid.Row="2"></TextBlock>
<TextBlock Text="Mark" Foreground="Red" Grid.Column="1" Grid.Row="3"></TextBlock>
<TextBlock Text="Model" Foreground="Red" Grid.Column="1" Grid.Row="4"></TextBlock>
<!-- Data-->
<TextBlock Text="{Binding SKU}" Foreground="Black" Grid.Column="1" Grid.Row="1"></TextBlock>
<TextBlock Text="{Binding ProductCode}" Foreground="Black" Grid.Column="1" Grid.Row="2"></TextBlock>
<TextBlock Text="{Binding Mark}" Foreground="Black" Grid.Column="1" Grid.Row="3"></TextBlock>
<TextBlock Text="{Binding Model}" Foreground="Black" Grid.Column="1" Grid.Row="4"></TextBlock>
</Grid>
</DataTemplate>
</Page.Resources>
<Grid x:Name="grdProduct" DataContext="Binding">
<ItemsControl>
<ItemsControl ItemTemplate="{StaticResource tmpGrdProducts}"></ItemsControl>
</ItemsControl>
</Grid>
source code
var Products = from t in bsEntity.ProductsTemps select t;
grdProduct.DataContext = Products;
Thank you.

You need to bind the ItemsSource property of your ItemsControl to the collection.
Also inside your items control, you've declared an extra items control with the item template.
What you're looking for is something like:
<ItemsControl ItemsSource="{Binding Path=YourCollectionProperty}">
<ItemsControl.ItemTemplate>
<!--your data template here-->
</ItemsControl.ItemTemplate>
</ItemsControl>
Also, I don't think you want DataContext="Binding" on your grid. If at all you'll probably need DataContext="{Binding}"

Related

C# WPF App-wide DataTemplate works on one page but not on the same page in a Datagrid

I have written a DataTemplate in App.xaml.
<DataTemplate x:Key="PlayDVBViewer">
<GroupBox Header="{x:Static properties:Resources.PlayInDVBViewer}" UseLayoutRounding="True" Margin="5">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Text="{x:Static properties:Resources.ChooseDVBViewer}" Margin="5" VerticalAlignment="Center"/>
<ComboBox Grid.Row="1" ItemsSource="{Binding Clients.Items}" SelectedItem="{Binding Client}" SelectedValuePath="Name" DisplayMemberPath="Name" Margin="5" Padding="5" VerticalAlignment="Center" Background="Transparent"/>
<!--<Button Grid.Column="1" Grid.Row="1" x:Name="btnPlay" Click="BtnPlay_Click" Margin="5" Padding="5" VerticalAlignment="Center" Background="Transparent" Content="{x:Static properties:Resources.Playback}"/>-->
<Button Grid.Column="1" Grid.Row="1" Command="{Binding BtnPlayClick}" Margin="5" Padding="5" VerticalAlignment="Center" Background="Transparent" Content="{x:Static properties:Resources.Playback}"/>
</Grid>
</GroupBox>
</DataTemplate>
The template works correctly in a page. But on the same page, in a DataGrid, it doesn't work. Why?
<!--Wiedergabe Optionen -->
<GroupBox Header="{x:Static properties:Resources.Playback}" Grid.Row="1" Grid.Column="2" Margin="5">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<ContentPresenter Grid.Row="0" ContentTemplate="{StaticResource ResourceKey=PlayDVBViewer}" Content="{Binding}"/>
</Grid>
</GroupBox>
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<Grid>
<!-- Detail Grid mit den Buttons zum Abspielen -->
<Grid Grid.Row="0">
<StackPanel Orientation="Horizontal">
<GroupBox Header="{x:Static properties:Resources.Picture}" MinWidth="120" Margin="5">
<Image Height="64" Source="{Binding ImagePath, Mode=OneWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:PageAufnahmen}}}" Margin="5"/>
</GroupBox>
<ContentPresenter ContentTemplate="{StaticResource PlayDVBViewer}"/>
</StackPanel>
</Grid>
</Grid>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
Ok, I have found the error.
If you use ContentPresenter in the DataTemplate of a grid. The binding will bind to the selected item. The solution is, use the same binding, like the image in the last box, see my post above.

dynamically create control using mvvm

I've been trying to create control dynamically and so far it is working. But my problem is the layout
<Grid Grid.Row="2" >
<ItemsControl IsTabStop="False" ItemsSource="{Binding ListControls}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="120*"/>
<RowDefinition Height="120*"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Label Content="AN:" Margin="5,5,5,5" FontSize="14" VerticalContentAlignment="Center"/>
<TextBox Grid.Column="1" FontSize="14" VerticalContentAlignment="Center" Margin="5,5,5,5"/>
</Grid>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
With the xaml above. this is the screenshot of the layout
and if i use a xaml like this
<Grid Grid.Row="2" >
<ItemsControl IsTabStop="False" ItemsSource="{Binding ListControls}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch">
<Label Content="AN:" Margin="5,5,5,5" FontSize="14" VerticalContentAlignment="Center"/>
<TextBox Width="100" FontSize="14" VerticalContentAlignment="Center" Margin="5,5,5,5"/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
But my goal is i want the textbox to expand if the program is maximized.
How can i adjust the xaml code in order to expand the textbox? Thank you
Directly use Grid instead of StackPanel also remove Width="100".
<Grid HorizontalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Content="AN:" Margin="5,5,5,5" FontSize="14" VerticalContentAlignment="Center"/>
<TextBox Grid.Column="1" FontSize="14" VerticalContentAlignment="Center" Margin="5,5,5,5"/>
</Grid>

ListView inside telerik GridView( or another listview ) in WPF(MVVM)

I have added a ListView in GridView
<tel:RadGridView AutoGenerateColumns="False" x:Name="ModuleGrid" ItemsSource="{Binding ProjectList,Source={StaticResource TowersVM},Mode=TwoWay}"
HorizontalAlignment="Stretch" ShowGroupPanel="False" ColumnWidth="*" ShowColumnHeaders="False" RowIndicatorVisibility="Collapsed" Height="500">
<tel:StyleManager.Theme>
<tel:Windows8TouchTheme/>
</tel:StyleManager.Theme>
<tel:RadGridView.Columns>
<tel:GridViewImageColumn Width="150" x:Name="Img" IsFilterable="False" DataMemberBinding="{Binding TowerImage}"/>
<!--<tel:GridViewDataColumn Width="100" x:Name="Img" Header="" IsFilterable="False" DataMemberBinding="{Binding TowerImage}"/>-->
<tel:GridViewColumn>
<tel:GridViewColumn.CellTemplate>
<DataTemplate>
<ListView x:Name="TemplateList" ItemsSource="{Binding UnitTypesList,Source={StaticResource TowersVM},Mode=TwoWay}">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"></StackPanel>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="40,20,40,20">
<Grid x:Name="TemplateGrid">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<StackPanel Grid.Row="0" Orientation="Vertical" Margin="5,0,0,0">
<TextBlock FontSize="14" Text="{Binding Tower_Name}" FontWeight="ExtraBold"/>
<TextBlock FontSize="9" Text="{Binding Location}"/>
</StackPanel>
<TextBlock Grid.Row="1" Text="{Binding Unit_type_desc}" Margin="5,5,0,0"/>
<StackPanel Grid.Row="2" Grid.Column="0" Orientation="Horizontal" Margin="5,5,0,0">
<Image Source="/Resources/rupee_icon.png" Width="30"/>
<StackPanel Orientation="Vertical" Margin="5,0,0,0">
<TextBlock Text="Price"/>
<TextBlock Text="{Binding List_price}">
<Run Text=" Cr."/>
</TextBlock>
</StackPanel>
</StackPanel>
<StackPanel Grid.Row="2" Grid.Column="1" Orientation="Horizontal" Margin="20,5,0,0">
<Image Source="/Resources/area_icon.png" Width="30"/>
<StackPanel Orientation="Vertical" Margin="5,0,0,0">
<TextBlock Text="Area"/>
<TextBlock Text="{Binding Carpet_area}">
<Run Text=" Sqft."/>
</TextBlock>
</StackPanel>
</StackPanel>
<Button BorderBrush="LightGreen" Background="White" Grid.Row="3" Grid.Column="0" Margin="0,5,0,5">
<StackPanel Orientation="Horizontal">
<TextBlock Text="Available : "/>
<TextBlock Text="{Binding Available,Source={StaticResource TowersVM},Mode=TwoWay}"/>
</StackPanel>
</Button>
<Button BorderBrush="Red" Background="White" Grid.Row="3" Grid.Column="1" Margin="20,5,0,5">
<StackPanel Orientation="Horizontal">
<TextBlock Text="Sold : "/>
<TextBlock Text="{Binding Sold,Source={StaticResource TowersVM},Mode=TwoWay}"/>
</StackPanel>
</Button>
</Grid>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</DataTemplate>
</tel:GridViewColumn.CellTemplate>
</tel:GridViewColumn>
</tel:RadGridView.Columns>
</tel:RadGridView>
What I want is :
A GridView row with one Item which will contain a ListView (with different number of Items).
The problem: All the RadGrid rows are having same number of Items in the ListView.
ViewModel code :
public async void PopulateProjects()
{
var vAllProjList = await _projectsRepo.GetAllAsync();
foreach(var proj in vAllProjList)
{
//UnitTypesList.Clear();
ProjectList.Add(new ProjectTowerUnitTypeModel() { Projectid = proj.Id, ProjectName = proj.Name });
var vProjSpecificUnitList = await _unitTypesRepo.FindAsync(q => q.Class_id == proj.Id);
foreach(var unit in vProjSpecificUnitList)
{
UnitTypesList.Add(new ProjectTowerUnitTypeModel() { });
}
}
}
I achieved it by taking ItemsSource Collection of child as a Model Property of Parent.

ListBox Windows phone

I want to display a list of element in a Grid. it contains two columns and two rows. Help me, please.
Item1 in Grid.row=0 Grid.column=0,
Item2 in Grid.row=0 Grid.column=1...
xaml code:
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Vertical" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Grid x:Name="AllDeal" >
<Grid.RowDefinitions>
<RowDefinition Height="240"></RowDefinition>
<RowDefinition Height="240"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="229"></ColumnDefinition>
<ColumnDefinition Width="229"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Border Background="#b09f4e"></Border>
<TextBlock Foreground="Black" FontSize="48" Margin="0,10,97,164" RenderTransformOrigin="0.546,0.074" >75%</TextBlock>
<TextBlock Foreground="White" Text="{Binding Title}" Margin="0,69,22,114"></TextBlock>
<TextBlock Text="{Binding numberday}" Foreground="Blue" Margin="168,176,0,0" FontSize="24"></TextBlock>
<TextBlock Text="DAYS" Foreground="Blue" Margin="168,198,0,218" Grid.RowSpan="2"/>
<Border Grid.Column="1" Background="#b8a54b"></Border>
<TextBlock Foreground="White" Grid.Column="1" Text="{Binding Title}" Margin="0,69,22,114"></TextBlock>
<TextBlock Foreground="Black" Grid.Column="1" FontSize="48" Margin="0,10,97,164" RenderTransformOrigin="0.546,0.074" >15%</TextBlock>
<TextBlock Text="{Binding numberday}" Grid.Column="1" Foreground="Blue" Margin="168,176,0,0" FontSize="24"></TextBlock>
<TextBlock Text="DAYS" Foreground="Blue" Grid.Column="1" Margin="168,198,0,218" Grid.RowSpan="2"/>
<Border Grid.Row="1" Background="#bfb274"></Border>
<Border Grid.Column="1" Grid.Row="1" Background="#c7bc85"></Border>
<ListBoxItem>
</ListBoxItem>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

How to add a border to UniformGrid cells

I'd like to add a border around elements in my WPF UniformGrid. What I've tried:
<Window.Resources>
<DataTemplate x:Key="GridCell">
<Border BorderBrush="DarkGray" BorderThickness="5"></Border>
</DataTemplate>
</Window.Resources>
...and...
<UniformGrid Name="grid">
<ItemsControl ItemTemplate="{StaticResource GridCell}"></ItemsControl>
</UniformGrid>
It doesn't work (no border appears). I'd like to have each child of the UniformGrid (buttons which are created programmatically, so they don't appear here) to have a border. It needs to look like, well, a grid... with horizontal and vertical gridlines delineating rows and columns.
<Grid>
<ItemsControl ItemsSource="{Binding NumericalPatches}" >
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Rows="7" Columns="7"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border BorderBrush="DimGray" BorderThickness="3">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Margin="5" Text="{Binding StringFormat=F4,Path=Red}" FontSize="14" Foreground="Red"/>
<TextBlock Grid.Row="0" Grid.Column="1" Margin="5" Text="{Binding StringFormat=F4,Path=Green}" FontSize="14" Foreground="Green"/>
<TextBlock Grid.Row="1" Grid.Column="0" Margin="5" Text="{Binding StringFormat=F4,Path=Blue}" FontSize="14" Foreground="Blue"/>
<TextBlock Grid.Row="1" Grid.Column="1" Margin="5" Text="{Binding StringFormat=F4,Path=Chroma}" FontSize="14" Foreground="White"/>
</Grid>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>

Categories