datatemplate

DataTemplate for a DataType - how to override this DataTemplate in a particular ListBox?

雨燕双飞 提交于 2019-12-05 21:36:29
I have created several DataTemplates for some of the DataTypes in my pet project. These data templates are really cool as they work like magic, magically transforming the look of the instances of the data types whenever and wherever they show up in the UI. Now I want to be able to change the DataTemplate for these DataTypes in one particular ListBox. Does this mean I have to stop relying on WPF automatically applying the data template to the data types and assign a x:Key to the DataTemplates and then apply the Template/ItemTemplate in the UI using that key? A ListBox contains items of various

C#/WPF: Get Binding Path of an Element in a DataTemplate

﹥>﹥吖頭↗ 提交于 2019-12-05 18:22:26
问题 How can I get the Binding Path of an Element in a DataTemplate? My XAML looks like this: <GridViewColumn Header="Double"> <GridViewColumn.CellTemplate> <DataTemplate> <TextBlock Text="{Binding TotalValues, Mode=OneWay, StringFormat=\{0:0\'0.00\}, Converter={StaticResource GridValueConverter}}" TextAlignment="Right" Width="auto"/> </DataTemplate> </GridViewColumn.CellTemplate> </GridViewColumn> <GridViewColumn Header="Comments" DisplayMemberBinding="{Binding Path=Comments, Mode=OneWay}" Width=

Datatemplate in c# code-behind

蹲街弑〆低调 提交于 2019-12-05 16:56:20
I search an option to build a datatemplate in c# code. I had used : DataTemplate dt = new DataTemplate(typeof(TextBox)); Binding bind = new Binding(); bind.Path = new PropertyPath("Text"); bind.Mode = BindingMode.TwoWay; FrameworkElementFactory txtElement = new FrameworkElementFactory(typeof(TextBox)); txtElement.SetBinding(TextBox.TextProperty, bind); txtElement.SetValue(TextBox.TextProperty, "test"); dt.VisualTree = txtElement; textBox1.Resources.Add(dt, null); But it doesn't work (it is placed at the Loaded-Method of the window - so my textbox should show the word "test" at window start).

Outside Property inside a DataTemplate WPF

时间秒杀一切 提交于 2019-12-05 16:27:25
Scenario: I have a ListBox and the ListBoxItems have a DataTemplate. What I want to do is put a ContextMenu in the DataTemplate. The catch is that I want this ContextMenu ItemsSource to be different depending on certain properties in the window. My initial thought is that I could just bind the ItemsSource to a Property in the window and that would return an ItemsSource; however, I cant seem to bind to this property correctly. I believe this is because I am in the DataTemplate and consequently the DataContext (I believe that is the right word) is of that ListBoxItem and not of the window. How

How can I retrieve the DataTemplate (and specific objects) of an item in an ItemsControl?

有些话、适合烂在心里 提交于 2019-12-05 13:33:59
I have seen solutions to a very similar issue, yet it doesn't translate to mine. (Namely, this article: http://blogs.msdn.com/wpfsdk/archive/2007/04/16/how-do-i-programmatically-interact-with-template-generated-elements-part-ii.aspx ) My ItemsControl is bound to an observable collection, which can have items dynamically added to it. When I add an item to the observable collection, the templated item renders properly in my itemscontrol, but I can't figure out how to access it. My my observable colleciton changed code, I am trying to access information about. I am using a custom

Why should we use DataTemplate.DataType

二次信任 提交于 2019-12-05 12:17:42
When I am creating a Resource we are specifying the DataType inside it: <Window.Resources> <DataTemplate x:Key="StudentView" DataType="this:StudentData"> <TextBox Text="{Binding Path=StudentFirstName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Grid.Row="1" Grid.Column="2" VerticalAlignment="Center" /> <TextBox Text="{Binding Path=StudentGradePointAverage}" Grid.Row="2" Grid.Column="2" VerticalAlignment="Center" /> </DataTemplate> <Window.Resources> And while binding : <ItemsControl ItemsSource="{Binding TheStudents}" ItemTemplate="{StaticResource StudentView}"> So why are we using the

Access Element inside Silverlight DataTemplate

帅比萌擦擦* 提交于 2019-12-05 11:42:57
I have not been able to find a clean solution to the following problem even though there are a few related questions already on SO. If I have a data template that is being used multiple times, for example, a TreeViewItem.HeaderTemplate, how can I change something the template for only some of the TreeViewItems. For example, let's say my TVI HeaderTemplate has a textblock and depending on the string, I want to make the fontweight bold. I want to do something like this: ((TextBlock)myTreeView.Items.ElementAt(0).FindName("myTextBlock")).FontWeight = FontWeights.Bold; Does anyone have a solution

When to use TemplateBinding and TemplatedParent in WPF

安稳与你 提交于 2019-12-05 06:59:23
I have a confusion over TemplateBinding and TemplatedParent. I have gone through this link also WPF TemplateBinding vs RelativeSource TemplatedParent But my doubt is when to use TemplateBinding and TemplatedParent? Thanks in advance. Rachel {TemplateBinding X} is simply a shortcut way of writing the {Binding X, RelativeSource={RelativeSource TemplatedParent}} . They evaluate to the same thing, although TemplateBinding is evaluated at compile-time while RelativeSource TemplatedParent is evaluated at run-time. Because it is evaluated at compile-time, TemplateBinding is a bit faster to evaluate

How do you use a DataType-targeted DataTemplate together with GridView?

不羁的心 提交于 2019-12-05 06:15:04
问题 Before you answer: This question is about WPF's ListView control (with GridView ). It is not about WPF's DataGrid control, ASP.NET's DataGrid or ListView controls, or WinForm's DataGridView or ListView controls. They all sound very similar and are often confused, but an answer for the wrong type of control is both unhelpful to me and more importantly a waste of the time you took to write the answer, which I believe should not be squandered. I have a ListView control with GridView that has

wpf listbox and datatemplate with grid inside

◇◆丶佛笑我妖孽 提交于 2019-12-05 05:21:17
问题 I have a following question. I want to have ListBox with DataTemplate as Grid . This grid has 2 two columns. I want to set first column width to 3* and another to * . How to do this? I will copy my code. <ListBox x:Name="commandListbox" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" HorizontalAlignment="Stretch"> <ListBox.ItemTemplate> <DataTemplate> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="3*" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <TextBlock Grid