datatemplate

How to Automatically Use a DataTemplate Based on ContentControl's Current Content's DataType

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-28 22:45:19
When I attempt to specify multiple DataTemplates for use by a ContentControl so that the correct one (based on Type) is used, I end up with Content that is simply the Content's ToString() value. <ContentControl DataContext="{Binding MyTreeRootViewModels}" Content="{Binding /, Path=CurrentlySelectedTreeViewModel}"> <ContentControl.Resources> <DataTemplate DataType="x:Type vm:TypeAViewModel"> <StackPanel> <local:TypeAUserControl /> </StackPanel> </DataTemplate> <DataTemplate DataType="x:Type vm:TypeBViewModel"> <StackPanel> <local:TypeBUserControl /> </StackPanel> </DataTemplate> <

Inline editing TextBlock in a ListBox with Data Template (WPF)

孤街醉人 提交于 2019-11-28 18:28:30
Using WPF, I have a ListBox control with a DataTemplate inside it. The relevant XAML code is shown below: <ListBox Name="_todoList" Grid.Row="1" BorderThickness="2" Drop="todoList_Drop" AllowDrop="True" HorizontalContentAlignment="Stretch" ScrollViewer.HorizontalScrollBarVisibility="Disabled" AlternationCount="2"> <ListBox.ItemTemplate> <DataTemplate> <Grid Margin="4"> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <CheckBox Grid.Column="0" Checked="CheckBox_Check" /> <TextBlock Name="descriptionBlock" Grid.Column="1" Text="

Display a default DataTemplate in a ContentControl when its content is null or empty?

▼魔方 西西 提交于 2019-11-28 17:12:16
I would think this is possible, but the obvious way isn't working. Currently, I'm doing this: <ContentControl Content="{Binding HurfView.EditedPart}"> <ContentControl.Resources> <Style TargetType="ContentControl" x:Key="emptytemplate"> <Style.Triggers> <DataTrigger Binding="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=Content}" Value="{x:Null}"> <Setter Property="ContentControl.Template"> <Setter.Value> <ControlTemplate> <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch"> <TextBlock>EMPTY!</TextBlock> </Grid> </ControlTemplate> </Setter.Value> </Setter> <

How can I bind an ObservableCollection of ViewModels to a MenuItem?

谁说胖子不能爱 提交于 2019-11-28 17:06:21
When I bind Menu Items with an ObservableCollection, only the "inner" area of the MenuItem is clickable: alt text http://tanguay.info/web/external/mvvmMenuItems.png In my View I have this menu: <Menu> <MenuItem Header="Options" ItemsSource="{Binding ManageMenuPageItemViewModels}" ItemTemplate="{StaticResource MainMenuTemplate}"/> </Menu> Then I bind it with this DataTemplate : <DataTemplate x:Key="MainMenuTemplate"> <MenuItem Header="{Binding Title}" Command="{Binding DataContext.SwitchPageCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Menu}}}" Background="Red"

Silverlight DataGrid.Celltemplate Binding to ViewModel

家住魔仙堡 提交于 2019-11-28 13:46:25
I am in the process of implimenting the MVVC pattern and am having trouble binding a property in the viewmodel from within a DataTemplate within a datagrid. If I have a textblock outside the DataTemplate in the column it works fine (since I am directly referencing the datacontext of the UserConrol, i.e. the VM) however from within the DataTemplate it wont return the plain text property. It will however return a property from the iterated IEnumerable item. <UserControl xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" x:Class="Timesheet.Silverlight.Modules

WPF TabStop / TabIndex in ItemsControl

前提是你 提交于 2019-11-28 13:12:21
I'm dynamically adding WPF ComboBox-es and I want to be able to select the added ComboBoxes with the 'TAB' key . (Dynamically generate TabIndex or something) As it actually is: What I want: How would you do that? This is the code: <ItemsControl ItemsSource="{Binding}" Name="myItemsControl"> <ItemsControl.ItemTemplate> <DataTemplate> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="3*"/> <ColumnDefinition Width="*"/> <ColumnDefinition Width="Auto"/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <ComboBox Grid.Column="0" ItemsSource

Using a datatemplate for the TabItems in a tabControl

本小妞迷上赌 提交于 2019-11-28 11:52:41
问题 If I have a class called: GuiObject, and that class has a list of GuiObjects called: "GuiObjects". Now say my window has a list of GuiObjects, which I use in the .xaml file to dataBind to: <StackPanel> <ItemsControl ItemsSource="{Binding TopObjectList}" DataTemplateSelector="{DynamicResource templateSelector"/> </StackPanel> I can make a datatemplate for every type of FrameworkElement I want to generate, but I'm having trouble with the TabControl. I can create a datatemplate for the

Explicitly refresh DataTemplate from a DataTemplateSelector?

血红的双手。 提交于 2019-11-28 10:49:24
I set up a ContentControl.DataTemplateSelector to my desired one. I want that according to a command or whatever, call the ContentControl to reselect the template from the selector by either xaml or code. Thank I'm not aware of any (non-kludgy) way to do this: the DataTemplateSelector is called when WPF needs to select the template, and that's a one-off decision as far as WPF is concerned. (You can kludge it by making WPF think the content has changed, e.g. by setting the content to null and then back again -- I think that would work but haven't tested it -- but this is pretty ugly!) If there

Specify Command for MenuItem in a DataTemplate

给你一囗甜甜゛ 提交于 2019-11-28 09:03:22
I have a context menu. It's bound to some collection and it has a defined ItemTemplate like this: <ContextMenu ItemsSource={Binding ...} ItemTemplate={StaticResource itemTemplate} /> itemTemplate is a simple DataTemplate with a TextBlock: <DataTemplate x:Key="itemTemplate"> <TextBlock Text={Binding ...} /> </DataTemplate> How do I bind Command property for MenuItem to the underlying object's property? I think you need to wrap your TextBlock in a MenuItem: <DataTemplate x:Key="itemTemplate"> <MenuItem Command={Binding ...}> <TextBlock Text={Binding ...} /> </MenuItem> </DataTemplate> But I don

Purpose of using FrameworkElementFactory

荒凉一梦 提交于 2019-11-28 08:28:51
问题 In one of the application I am working I have found this code - public class MatrixCellTemplate : ColumnDataTemplate<MatrixCellContainer> { } public class ColumnDataTemplate<T> : DataTemplate where T : FrameworkElement { public ColumnDataTemplate() { FrameworkElementFactory factory = new FrameworkElementFactory(typeof(T)); VisualTree = factory; } } This MatrixCellTemplate is used to set the CellTemplate of a custom DataGridTemplateColumn (later added to DataGrid.Columns collection) like this