In XAML, I have:
<DataTemplate x:Key="AgeItemTemplate">
<Border BorderThickness="0,0,0,0" BorderBrush="#6FBDE8">
<TextBlock Margin="2" Text="{Binding Age}" VerticalAlignment="Center" Grid.Column="1" />
</Border>
</DataTemplate>
How could I use that DataTemplate in code?
I know I can create a new template and linked to a gridview column but I want to define that template in xaml. Is there any way to modify and use that dataTemplate in code behind?
You need to use the findresource method on FrameworkElement.
<DataTemplate x:Key="PersonItemTemplate" x:Name="someTemplate">
<Border BorderThickness="0,0,0,0" BorderBrush="#6FBDE8">
<Grid Margin="2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="32" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Image Source="Images/person.png" Width="24" Height="24" Grid.Column="0" HorizontalAlignment="Center" />
<TextBlock Text="{Binding Name}" VerticalAlignment="Center" Grid.Column="1" />
</Grid>
</Border>
</DataTemplate>
code behind:
template1 = (DataTemplate)FindName("someTemplate");
linkColumn1 = new GridViewColumn
{
Header = "Test",
CellTemplate = template1,
//Width = 88, // Comment out to set to auto
};
gv.Columns.Add(linkColumn1);
as a result I was able to duplicate a column with code:
this is helpful to populate a listview dynamically because it is harder to create the styles on code I believe.
Related
So, i have the following xaml at the moment-
<Window.Resources>
<DataTemplate x:Key="MailTemplate">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Date}" Width="150"/>
<TextBlock Text="{Binding Path=Subject}" Width="300"/>
<TextBlock Text="{Binding Path=From.DisplayName}" Width="125"/>
</StackPanel>
</DataTemplate>
</Window.Resources>
It's a template of a ListBox item.
The actual details do not really matter, its just about the Width attribute.
Is there a way to access the window's size in order to set the width? I don't want the Width to be constant, i want the TextBlocks to be able to change sizes if you stretch the window.
You can do that by not setting any fixed widths on the TextBlock elements, and use a Grid with star-sizing instead of a StackPanel as container. For example like this:
<DataTemplate x:Key="MailTemplate">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="150*" />
<ColumnDefinition Width="300*" />
<ColumnDefinition Width="125*" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="{Binding Path=Date}" />
<TextBlock Grid.Column="1" Text="{Binding Path=Subject}" />
<TextBlock Grid.Column="2" Text="{Binding Path=From.DisplayName}" />
</Grid>
</DataTemplate>
Currently I am implementing an app using HamburgerControlMenu from Mahapps.Metro toolkit.
I need to focus a specific HambugerMenu Item by code, after an event.
This is the WPF code:
<Grid.Resources>
<DataTemplate x:Name="aa" x:Key="MenuItemTemplate" DataType="{x:Type viewModels:MenuItem}">
<Grid x:Name="gridMain" Height="48">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="48" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<ContentControl Grid.Column="0"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Foreground="White"
Focusable="False"
Content="{Binding Icon}"/>
<TextBlock x:Name="txtBlockMenu"
Grid.Column="1"
VerticalAlignment="Center"
FontSize="16"
Foreground="White"
Text="{Binding Text}" />
</Grid>
</DataTemplate>
</Grid.Resources>
<controls:HamburgerMenu x:Name="HamburgerMenuControl"
Foreground="White"
PaneBackground="#FF444444"
IsPaneOpen="False"
ItemsSource="{Binding Menu}"
OptionsItemsSource="{Binding OptionsMenu}"
ItemClick="HamburgerMenuControl_OnItemClick"
OptionsItemClick="HamburgerMenuControl_OnItemClick"
ItemTemplate="{StaticResource MenuItemTemplate}"
OptionsItemTemplate="{StaticResource MenuItemTemplate}"/>
Little help would be great.
Set the SelectedIndex or SelectedItem property. The following will for example select the second item at index 1:
HamburgerMenuControl.IsPaneOpen = true;
HamburgerMenuControl.SelectedIndex = 1;
you can set it using :
this.*yourHamburgerControlName*.SelectedIndex = *InsertHere the position of your HamburgerMenuItem in the ItemSource*;
I need to binding objects of the lists below in a ItemsControl binded in a ScrollViewer in wpf .
I Provand assigning a path but I still can not binding, maybe I'm wrong ? In the subject of the first level the bind is successful , but when I go down in the lists below of the same object bind will not work.
Xaml Scrollviewer:
<surface:SurfaceScrollViewer Grid.Row="2" VerticalScrollBarVisibility="Hidden" Background="#fff" PanningMode="VerticalOnly">
<ItemsControl x:Name="scrollViewerFolderItemsSource" ItemsSource="{Binding Path=companies}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<surface:SurfaceButton Tag="{Binding CPID}" Click="Open_Click" Grid.ColumnSpan="2">
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<Border BorderThickness="0,1,0,0" BorderBrush="Gray" Height="57" Background="White">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="80"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="60"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0" Background="#fff"></Grid>
<Image Grid.Row="0" Grid.Column="0" Width="32" VerticalAlignment="Center" HorizontalAlignment="Center" Source="{Binding ImageFolder}"></Image>
<TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding CompanyName}" Foreground="#565656" FontFamily="{StaticResource Lato Semibold}" HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="10" Margin="10,3,0,0" Style="{DynamicResource Lato-Semibold}"/>
<TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding companies.Attachments.Name}" Foreground="#565656" FontFamily="{StaticResource Lato Semibold}" HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="10" Margin="10,3,0,0" Style="{DynamicResource Lato-Semibold}"/>
<TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding AttachmentFolders.Name}" Foreground="#565656" FontFamily="{StaticResource Lato Semibold}" HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="10" Margin="10,3,0,0" Style="{DynamicResource Lato-Semibold}"/>
</Grid>
</Border>
</ControlTemplate>
</Button.Template>
</surface:SurfaceButton>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</surface:SurfaceScrollViewer>
CodeBehind view list binded:
My target is binding Text="{Binding companies.Attachment.Name}"
If i print Text="{Binding Attachment}" my result print on deploy is "(Collection)", why print Attachment.Name ?
Attachments is a Collection, to visualize a collection you should use a ListBox and use this binding ItemsSource="{Binding companies.Attachment}", you also need to define the ItemTemplate for the ListBox.
With the ListBox you are able to visualize all the element, but if you want to show just the first attachment name you can use this binding Text="{Binding companies.Attachment[0].Name}"
or another solution could be to create a new property called AttachmentToShow of type Attachment and use this binding
Text="{Binding AttachmentToShow.Name}"
with this solution updating AttachmentToShow will result on an UI update.
Each list item must be
TextBlock 1 fill first row with 100% width;
TextBlock 2,3,4 must fill 33% each on separate row;
Why TextBlock 2,3,4 not strech?
<ListView.ItemTemplate><DataTemplate><StackPanel>
<TextBlock Text="{Binding Name}" />
<Grid HorizontalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock TextAlignment="Right" Grid.Row="0" Grid.Column="0"
HorizontalAlignment="Stretch" VerticalAlignment="Center"
Text="{Binding Rest}" FontSize="28"/>
<TextBlock TextAlignment="Right" Grid.Row="0" Grid.Column="1"
HorizontalAlignment="Stretch" VerticalAlignment="Center"
Text="{Binding Currency.Name}" FontSize="25"/>
<TextBlock TextAlignment="Right" Grid.Row="0" Grid.Column="2"
HorizontalAlignment="Stretch" VerticalAlignment="Center"
Text="{Binding FullRest}" FontSize="22"/>
</Grid>
</StackPanel></DataTemplate></ListView.ItemTemplate>
P.S.
How i can add 1...x rows in list view in design time?
Use ListView.ItemContainerStyle
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListView.ItemContainerStyle>
This code (with normal values, because I don't have the underlying data structure) works fine for me.
Different rows can have different widths however. That can be fixed by setting the Width of the StackPanel to the ActualWidth - margins of the listview.
You can find some info about how to add mock data, for usage in the designer, here: How to get mock data into listview during design time and real data at run time in WPF
I used a ListBox to bind my data in similar way.
You will need to give stackpanel specific width
And you will have to set textwrapping property to 'no wrap'
<ListBox x:Name="listbox" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Width="480">
<TextBlock Text="{Binding main}" />
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock TextWrapping="NoWrap" Text="{Binding one}"/>
<TextBlock TextWrapping="NoWrap" Text="{Binding two}" Grid.Column="1"/>
<TextBlock TextWrapping="NoWrap" Text="{Binding thr}" Grid.Column="2"/>
</Grid>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
This Works perfectly for me!
I have a style in my Silverlight 4 app. where I define column names in a grid, is it possible to get these text of the headers from an object with this style? In the code below, what I would like to get is the strings "foo" and "bar", from an object with this style applied.
</Grid>
<Grid
x:Name="m_Headers"
Visibility="Visible"
Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition
Width="{StaticResource DataColunmWidth}" />
<ColumnDefinition
Width="{StaticResource DataColunmWidth}" />
</Grid.ColumnDefinitions>
<TextBlock
Margin="3,0,0,0"
Text="foo"
Grid.Column="1" />
<TextBlock
Margin="3,0,0,0"
Text="bar"
Grid.Column="2" />
</Grid>
I don't think you can do this with pure XAML, you'd have to duplicate the templates or create a custom control which exposes the text values as dependency properties via the template.
The best solution I think is to set up bindings for your Grid headers and fill them using your ViewModel:
<Grid
x:Name="m_Headers"
Visibility="Visible"
Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition
Width="{StaticResource DataColunmWidth}" />
<ColumnDefinition
Width="{StaticResource DataColunmWidth}" />
</Grid.ColumnDefinitions>
<TextBlock
Margin="3,0,0,0"
Text="{Binding FooText}"
Grid.Column="1" />
<TextBlock
Margin="3,0,0,0"
Text="{Binding BarText}"
Grid.Column="2" />
</Grid>