datatemplate

RelativeSource in DataTemplate works with TabControl but not with TabItem

删除回忆录丶 提交于 2019-11-29 15:10:36
I am having a TabControl and within it a TabItem having a ContentControl. This ContentControl is applied a datatemplate. The code is here: <TabControl x:Name="tabControl1" Tag="Giving URI here works"> <TabItem x:Name="tabItem1" Tag="Giving URI here doesnt work"> <ContentControl ContentTemplate="{StaticResource myOptionsDataTemplate}"> <StackPanel> <TextBlock Text="Some Text" /> </StackPanel> </ContentControl> </TabItem> </TabControl> And the data template is: <DataTemplate x:Key="myOptionsDataTemplate"> <Border> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height

How do I set a DataTemplate for a WPF TreeView to display all Elements of an List?

隐身守侯 提交于 2019-11-29 14:26:11
I'd like to visualize the following data structure using TreeViews in WPF: class MyDataContext { ICollectionView Outers {get;set;} //... } class Outer { string Name {get;set;} IEnumberable<Inner> Actions {get;set;} } class Inner { string Description {get;set;} Command OnClick {get;set;} } This is my attempt so far: <!-- DataContext is MyDataContext at this point --> <TreeView ItemsSource="{Binding Path=Outers}"> <TreeView.Resources> <DataTemplate DataType="{x:Type myns:Outer}"> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding Path=Name}"/> <TreeView ItemsSource="{Binding Path

'Cannot find governing FrameworkElement…' warning when binding inside DataTemplates

泄露秘密 提交于 2019-11-29 13:13:03
I'm getting this warning in Visual Studio output window when binding on a SolidColorBrush property inside a DataTemplate : System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=MyColor; DataItem=null; target element is 'SolidColorBrush' (HashCode=22943289); target property is 'Color' (type 'Color') If I bind directly on the rectangle element, outside the DataTemplate , it all works well. Can anyone explain why this difference in the two apparently similar usages from the sample code below: My View:

Styling WPF ComboBox items

与世无争的帅哥 提交于 2019-11-29 13:12:13
I have a very simple WPF application which displays a ComboBox which binds to a list of classes which represent people. Each 'Person' object has a Name string field, and a Sex enum. I would like the ComboBox to display a drop-down of the various people's Name field, but for each line to be styled according to the Sex field, for example, blue for males, pink for females. Can anyone tell me what I am doing wrong? Here is the XML: <Window x:Class="ComboBoxColour.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Access Elements inside a DataTemplate… How to for more than 1 DataTemplate?

匆匆过客 提交于 2019-11-29 12:39:34
I've got 2 DataTemplates defined for a Listbox Control. 1 Template is for the UnSelected State and the other one is for the Selected State(showing more detail than the UnSelected State). I followed the example here: http://blogs.msdn.com/b/wpfsdk/archive/2007/04/16/how-do-i-programmatically-interact-with-template-generated-elements-part-ii.aspx about how to access the Elements inside the DataTemplates from Code behind. I get it right, but it only finds and returns an element of the UnSelected DataTemplate. But when i search for an element in the Selected DataTemplate i get a

How to access any control inside Hubsection Datatemplate in Windows 8.1 store

非 Y 不嫁゛ 提交于 2019-11-29 10:01:33
Please tell me how to access flipview control inside Hubsection * DataTemplate * Pedro Cavaleiro I don't know if you managed to solve your problem already. If you didn't here is how. private DependencyObject FindChildControl<T>(DependencyObject control, string ctrlName) { int childNumber = VisualTreeHelper.GetChildrenCount(control); for (int i = 0; i < childNumber; i++) { DependencyObject child = VisualTreeHelper.GetChild(control, i); FrameworkElement fe = child as FrameworkElement; // Not a framework element or is null if (fe == null) return null; if (child is T && fe.Name == ctrlName) { //

is there a datatemplate for grid panel elements in WPF?

强颜欢笑 提交于 2019-11-29 09:58:39
问题 Fairly new to WPF... I have a collection of data I would like to bind to a grid panel. Each object contains its grid row and column, as well as stuff to fill in at the grid location. I really like how I can create data templates in the listbox XAML to create a UI with almost nothing in the code behind for it. Is there a way to create a data template for grid panel elements, and bind the panel to a data collection? 回答1: You can use an ItemsControl with a Grid as its panel. Here is an example.

Selecting a data template based on type

旧巷老猫 提交于 2019-11-29 09:42:08
I've declared the following types: public interface ITest { } public class ClassOne : ITest { } public class ClassTwo : ITest { } In my viewmodel I'm declaring and initializing the following collection: public class ViewModel { public ObservableCollection<ITest> Coll { get; set; } = new ObservableCollection<ITest> { new ClassOne(), new ClassTwo() }; } In my view I'm declaring the following ItemsControl <ItemsControl ItemsSource="{Binding Coll}"> <ItemsControl.Resources> <DataTemplate DataType="local:ClassOne"> <Rectangle Width="50" Height="50" Fill="Red" /> </DataTemplate> <DataTemplate

WPF Implicit selection of template using DataTemplate, but outside of 'List'

混江龙づ霸主 提交于 2019-11-29 08:50:21
In my project, I have TreeView, which contains a tree of objects of various types (all subclassed from the same superclass). To the right of my TreeView I would like to have a "panel" (at the moment I just have a Grid) which displays information about the object currently selected in the tree. I want to use DataTemplate, as in the second example on this page , to adapt the layout & content of my "panel" based on the subclass type; however, I cannot find a suitable container (as I don't want a list control - I want to change my display for one item based on the selection in the treeview). This

multiple userControl instances in tabControl

风流意气都作罢 提交于 2019-11-29 08:06:35
I have a tabControl that is bound to an observable collection. In the headerTemplate, I would like to bind to a string property, and in the contentTemplate I have placed a user-control. Here's the code for the MainWindow.xaml: <Grid> <Grid.Resources> <DataTemplate x:Key="contentTemplate"> <local:UserControl1 /> </DataTemplate> <DataTemplate x:Key="itemTemplate"> <Label Content="{Binding Path=Name}" /> </DataTemplate> </Grid.Resources> <TabControl IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding Path=Pages}" ItemTemplate="{StaticResource itemTemplate}" ContentTemplate="{StaticResource