我有一个上下文菜单.它绑定到一些集合,它有一个定义的ItemTemplate,如下所示:
<ContextMenu
ItemsSource={Binding ...}
ItemTemplate={StaticResource itemTemplate}
/>
Run Code Online (Sandbox Code Playgroud)
itemTemplate是一个带TextBlock的简单DataTemplate:
<DataTemplate x:Key="itemTemplate">
<TextBlock Text={Binding ...} />
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
如何将MenuItem的Command属性绑定到底层对象的属性?
我将ContentControl.DataTemplateSelector设置为我想要的.
我希望根据命令或其他任何东西,调用ContentControl以通过xaml或代码从选择器重新选择模板.
谢谢
我有一个名为的视图模型
ViewModelClass
Run Code Online (Sandbox Code Playgroud)
它包含一个布尔值.
我有另一个包含的视图模型
ObservableCollection<ViewModelClass> m_allProjects;
Run Code Online (Sandbox Code Playgroud)
然后我在我看来有这个:
<DataTemplate>
<views:ProjectInfoView x:Key="ProjectInfoDetailTemplate"/>
</DataTemplate>
<ItemsControl Grid.Row="1" Grid.Column="0"
ItemsSource="{Binding AllProjects}"
ItemTemplate="{StaticResource ProjectInfoDetailTemplate}"
Margin="10,28.977,10,10">
</ItemsControl >
Run Code Online (Sandbox Code Playgroud)
现在我希望,基于AllProjects集合中的布尔值,使用不同的datatemplate.做这个的最好方式是什么?
我知道我可以用不同的ViewModel做这个并使用一种基于ViewModel的对象,但我更喜欢使用1个视图模型.
编辑:
我想用数据触发器来做这件事.有人可以提供一些代码吗?
我有一个数据模板,我在项目控制中使用,我想知道它是否可能在单项显示上使用它(模板)而没有项目控制?
如果不是最好的方法吗?
我在app.xaml中有一个DataTemplate,它将视图绑定到viewmodel.
<Application.Resources>
<DataTemplate DataType="{x:Type vm:someviewmodeltype}">
<vw:somevwcontrol />
</DataTemplate>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)
如果没有样式,则不会应用上述模板.我放风格的那一刻,像...
<Application.Resources>
<DataTemplate DataType="{x:Type vm:someviewmodeltype}">
<vw:somevwcontrol />
</DataTemplate>
<Style TargetType="TextBlock">
<Setter Property="FontSize" Value="20"></Setter>
</Style>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)
datatemplate被拿起并产生预期的结果......我不确定那里发生了什么......有人可以澄清一下吗?
谢谢.
我有一个ObservableCollection,它包含多种类型的视图模型,我想为每个GridViewColumn的CellTemplates中的每个类型创建一个DataTemplate.在这个简单的例子中,我可以创建一个基本的ViewModel,但我希望能够从xaml中完成这个.下面的xaml显示了我想要做的事情,其中一个DataTemplates将用于每个CellTemplate.
如果有一个GridViewColumn.Resources我会在那里定义DataTemplates然后在CellTemplate中使用DataTemplate和ContentPresenter,但我显然不能这样做.我想我可能需要一个TemplateSelector,但我不知道从哪里开始.
<ListView ItemsSource={Binding GenericObservableCollection>
<ListView.View>
<GridView>
<GridViewColumn Header="Type">
<GridViewColumn.CellTemplate>
<DataTemplate DataType="{x:Type vm:ActionInputViewModel}">
<TextBlock Text="Input"/>
</DataTemplate>
<DataTemplate DataType="{x:Type vm:ActionOutputViewModel}">
<TextBlock Text="Output"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Value">
<GridViewColumn.CellTemplate>
<DataTemplate DataType="{x:Type vm:ActionInputViewModel}">
<TextBlock Text="{Binding Property1}"/>
</DataTemplate>
<DataTemplate DataType="{x:Type vm:ActionOutputViewModel}">
<TextBlock Text="{Binding Property2}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
Run Code Online (Sandbox Code Playgroud) 如何为ViewModel可混合(可在表达式混合中设计)创建Datatemplate.当我转到资源并尝试直接编辑DataTemplate时,我在Drawingborad上看到的是一个空白矩形.这是因为DataTemplate没有绑定任何东西.当然我可以创建一个UserControl并在那里的代码中创建一些设计时数据来查看模板,但我现在必须在资源(编辑)和用户控件(查看我的编辑结果)之间来回切换.是不是有更直接的方式来编辑和查看我的DataTemplate?
我有一个类似的东西:
public class Section
{
private IEnumerable<Section> sections;
private IEnumerable<KeyValuePair<string, string>> attributes
public string Name {get;set;}
public IEnumerable<Section> Sections
{
get {return sections;}
}
public IEnumerable<KeyValuePair<string, string>> Attributes
{
get {return attributes;}
}
public Section(...)
{
//constructor logic
}
}
Run Code Online (Sandbox Code Playgroud)
其中包含在另一个类中,我们OtherClass
为了参数而调用它,它包装它并在ObjectDataProvider中的WPF中使用.
如您所见,一个实例Section
可以包含许多其他实例Section
.
在XAML中有没有办法创建一个处理这种递归的模板?
这是我到目前为止所得到的:
<UserControl x:Class="OtherClassEditor"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:OtherClassNS="clr-namespace:NameSpace"
Height="300" Width="300">
<UserControl.Resources>
<ObjectDataProvider ObjectType="{x:Type OtherClassNS:OtherClass}" x:Key="ViewModel" />
<DataTemplate x:Key="listViewAttr">
<WrapPanel>
<TextBlock Text="{Binding Path=Key}"></TextBlock><TextBlock Margin="0,0,4,0">: </TextBlock>
<TextBlock Text="{Binding Path=Value}"></TextBlock>
</WrapPanel>
</DataTemplate>
<DataTemplate x:Key="listViewSection">
<StackPanel>
<WrapPanel …
Run Code Online (Sandbox Code Playgroud) 我有一个资源字典:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="wpfUI2.MainWindowEvents">
<DataTemplate
x:Key="WorkspacesTemplate">
<TabControl
x:Name="Tab1"
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding}"
ItemTemplate="{StaticResource ClosableTabItemTemplate}"
Margin="4"/>
</DataTemplate>
...
Run Code Online (Sandbox Code Playgroud)
我想为TabControl添加一个事件处理程序.MainWindowEvents是在没有其他类的文件中定义的类:
Namespace wpfUI2
Public Class MainWindowEvents
End Class
End Namespace
Run Code Online (Sandbox Code Playgroud)
当我去添加一个事件处理程序时
<TabControl
x:Name="Tab1"
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding}"
ItemTemplate="{StaticResource ClosableTabItemTemplate}"
Margin="4"
SelectionChanged=""
/>
Run Code Online (Sandbox Code Playgroud)
并尝试在""之间单击以创建事件我收到一条错误,指出x:Class属性指定的类必须是文件中的第一个.好吧!奇怪的是,当我手动创建处理程序时:
Namespace wpfUI2
Public Class MainWindowEvents
Public Sub Tab1_SelectionChanged(sender As System.Object, e As System.Windows.Controls.SelectionChangedEventArgs)
End Sub
End Class
End Namespace
Run Code Online (Sandbox Code Playgroud)
一切都编译好,但我在window.show上得到一个运行时异常
我究竟做错了什么?
我想在运行时在代码中执行以下操作:
<DataTemplate x:Key="lightGreenRectangle">
<Rectangle Fill="LightGreen"/>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
到目前为止我有:
public DataTemplate GetColouredRectangleInDataTemplate(Color colour)
{
DataTemplate dataTemplate = new dataTemplate();
return dataTemplate;
}
Run Code Online (Sandbox Code Playgroud)
救命?我知道这不是设计控件样式的最优雅方式,但我想指定颜色的组件有一个名为"PointTemplate"的属性,类型为DataTemplate.
datatemplate ×10
wpf ×10
xaml ×3
app.xaml ×1
celltemplate ×1
command ×1
data-binding ×1
styles ×1
telerik ×1