我有一个enum让我们说
enum MyEnum
{
FirstImage,
SecondImage,
ThirdImage,
FourthImage
};
Run Code Online (Sandbox Code Playgroud)
我已将这个Enum绑定到XAML中的组合框中.
在定义组合框时,我已经定义了一个combox的ItemTemplate来获取两个UI元素:
我在XAML中做了这么多.
我想知道在哪里可以指定与组合框中的每个Enum值项目相对应的图像?这可能通过数据触发吗?
如果有人为此场景设置了XAML,我真的很感激.
提前谢谢了
我已经制作了一个示例演示VS 2010 RC示例项目,因为在我的生产项目中我使用MVVM时遇到了同样的错误.
在我的示例演示项目中,我只使用Code-behind而没有第三方依赖,因此您可以在此处下载演示项目并自行运行:http://www.sendspace.com/file/mwx7wv
现在问题:当我点击女孩/男孩按钮时,它应该切换datatemplate,不是吗?
我错了什么?
好的,我在这里也提供了一个代码片段:
代码隐藏在MainWindow.cs:
namespace ContentTemplateSelectorDemo
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
Person person;
public MainWindow()
{
InitializeComponent();
person = new Person(){ Gender = "xxx"};
person.IsBoy = true;
ContentGrid.DataContext = person;
}
private void btnBoys_Click(object sender, RoutedEventArgs e)
{
person.IsBoy = true;
person.IsGirl = false;
this.ContentGrid.DataContext = person;
}
private void btnGirls_Click(object sender, RoutedEventArgs e)
{
person.IsGirl = true;
person.IsBoy = …Run Code Online (Sandbox Code Playgroud) 我尝试使用祖先的对象设置Foreground颜色,但它没有任何效果.我甚至使用了更改超链接前景的提示而没有丢失悬停颜色,但它没有任何区别 - 我仍然得到一个悬停时为红色的蓝色超链接.HyperlinkStyleResourcesBasedOn
这是我的控件的XAML,包括ItemsControl使用超链接显示其项目的XAML :
<StackPanel Background="Red" TextElement.Foreground="White">
<StackPanel.Resources>
<Style TargetType="Hyperlink" BasedOn="{StaticResource {x:Type Hyperlink}}">
<Setter Property="Foreground" Value="Yellow"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Foreground" Value="White"/>
</Trigger>
</Style.Triggers>
</Style>
</StackPanel.Resources>
<TextBlock>Data import errors</TextBlock>
<ItemsControl ItemsSource="{Binding Errors}"/>
</StackPanel>
Run Code Online (Sandbox Code Playgroud)
而且这些项目ItemsControl正在取得以下成果DataTemplate:
<DataTemplate DataType="{x:Type Importer:ConversionDetailsMessage}">
<TextBlock>
<Run Text="{Binding Message, Mode=OneTime}"/>
<Hyperlink Command="Common:ImportDataCommands.ExplainConversionMessage" CommandParameter="{Binding}">
<Run Text="{Binding HelpLink.Item2, Mode=OneTime}"/>
</Hyperlink>
</TextBlock>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
值得一提的,那就是,我不想只是上直接设置不同颜色Hyperlink的DataTemplate.这是因为模板将被许多不同的ItemsControl对象使用,其中大部分将在白色背景上,因此可以使用标准的超链接颜色.(请注意,上面XAML中的那个具有红色背景.)
简而言之,我不希望 …
我有以下用户控件:
<TabItem
x:Name="Self"
x:Class="App.MyTabItem"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:app="clr-namespace:App"
>
<TabItem.Header>
<!-- This works -->
<TextBlock Text="{Binding ElementName=Self, Path=ShortLabel, UpdateSourceTrigger=PropertyChanged}"/>
</TabItem.Header>
<TabItem.ContentTemplate>
<DataTemplate>
<!-- This binds to "Self" in the surrounding window's namespace -->
<TextBlock Text="{Binding ElementName=Self, Path=ShortLabel, UpdateSourceTrigger=PropertyChanged}"/>
Run Code Online (Sandbox Code Playgroud)
这个自定义TabItem定义了一个DependencyProperty'ShortLabel'来实现一个接口.我想结合这一点,从内的其他性质TabItem的DataTemplate.但由于奇怪的相互作用,在TextBlock内部DataTemplate被绑定到父容器的TabItem,这也被称为"自我",但在另一个XAML文件中定义.
为什么Binding在TabItem.Header中工作,而不是在TabItem.ContentTemplate中工作,我应该如何从DataTemplate中获取用户控件的属性?
TemplateBinding:尝试绑定到内容的内容中的ContentPresenter TabItem.FindAncestor, AncestorType={x:Type TabItem}:找不到TabItem父母.当我指定MyTabItem类型时,这也不起作用.ElementName=Self:尝试绑定到具有该名称的控件在错误的范围内(父容器,而不是TabItem).我认为这给出了一个提示,为什么这不起作用:DataTemplate不是在XAML中定义的位置创建的,但显然是由父容器创建的.我假设我可以替换整体ControlTemplate来实现我正在寻找的效果,但由于我想保留默认的外观和感觉TabItem而不必保持整体ControlTemplate,我非常不愿意这样做.
同时我发现问题是:如果包含s …
我有以下内容DataTemplate:
<DataTemplate x:Key="ColoringLabels">
<TextBlock Padding="0"
Margin="0"
Name="Username"
Text="{Binding Username}"
Foreground="Gray"
FontStyle="Italic"
/>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding IsLoggedIn}" Value="True">
<Setter TargetName="Username" Property="FontSize" Value="14"/>
<Setter TargetName="Username" Property="Foreground" Value="Green"/>
<Setter TargetName="Username" Property="FontStyle" Value="Normal"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
我想ListView在每个用户名后跟一个模板的地方使用模板; 和一个空间.
实际上,模板的行为就像这样写:
<DataTemplate x:Key="ColoringLabels">
<TextBlock Padding="0"
Margin="0"
Name="Username"
Text="{Binding Username, StringFormat='{}{0}; '}"
Foreground="Gray"
FontStyle="Italic"
/>
<DataTemplate.Triggers>
...
</DataTemplate.Triggers>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
如何扩展原始模板以获得第二个模板的结果?
Silverlight 4已经出局了,似乎我们再次错过了这个版本中的DataTemplate DataType功能,这对于MVVM支持IMHO来说是非常关键的.对于我的WPF应用程序,此时,我已经非常习惯将我的Views的DataTemplates全局添加到我的Application.Resources,其中DataTypes用于我的相应ViewModel:
即.
<DataTemplate DataType="{x:Type viewModels:myViewModel}">
<views:myView/>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
我喜欢这种方法,因为我所有绑定的ViewModel都会自动显示正确的内容...当我在视图中将某些ItemSource绑定到ViewModels集合时尤其有用...例如,这将自动确保每个选项卡中的每个选项卡TabControl绑定到a Collection<SomeViewModel>显示与之关联的视图SomeViewModel.
我为SL 3尝试过的一些事情包括:
创建"DataTemplatePresenterContentControl",在控件加载时自动为内容应用DataTemplate
使用TypeConverter,动态应用于控制负载,沿着可视树向下查找数据绑定对象
使用动态应用于控件加载的样式,沿着可视树向下查找数据绑定对象
但是,这些方法都没有真正以可接受的方式解决我上面提到的情况,这非常关键.
因此,由于Silverlight 4中仍然无法开箱即用,我很高兴知道是否有人提出了一些合理的替代方案.
谢谢.
当人们无法使用Expression Blend时,人们如何找到.NET控件的默认模板?
到目前为止,当我需要WPF控件的模板时,我访问了Codeplex上的WPF项目页面,浏览了源代码,并且通常在"Generic.xaml"文件中找到了默认模板.
但现在我正在寻找"Frame"控件的默认模板.我在Codeplex上找不到它,我也没有在MS提供的.NET框架源中看到它(没有提供XAML文件,只有.cs文件).
Style Snooper工具提供了一个默认模板,但它似乎是逆向工程(不必要的冗长和引用内部类)而不是原始的清晰定义.
那么,穷人如何获得这些模板呢?
在MainWindow,我们有:
<HeaderedContentControl
Content="{Binding Path=Workspaces}"
ContentTemplate="{StaticResource WorkspacesTemplate}"
Header="Workspaces"
Style="{StaticResource MainHCCStyle}"
/>
Run Code Online (Sandbox Code Playgroud)
在资源中:
<DataTemplate x:Key="WorkspacesTemplate">
<TabControl
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding}"
ItemTemplate="{StaticResource ClosableTabItemTemplate}"
Margin="4"
/>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
在文章中说:
类型化的DataTemplate没有分配给它的x:Key值,但它的DataType属性设置为Type类的实例.如果WPF尝试呈现您的一个ViewModel对象,它将检查资源系统是否在作用域中具有类型化的DataTemplate,其DataType与ViewModel对象类型的(或基类)相同.如果找到一个,它将使用该模板呈现选项卡项的Content属性引用的ViewModel对象.
我的问题是:
模板如何知道类型是工作空间的集合(WorkspaceViewModel)?
我有一个longlistselector与我在这个longlistselector中添加的某些图像.我还更改了此图像的边距以使图像上下移动等等.但我想把这张图片放在这个longlistselector的另一张图片的前面.我尝试过使用Canvas.Zindex.我已经尝试在网格级别,图像级别和longlistselector的顶层设置它.()但它仍然无效.有人有想法吗?您可以在下面找到我的代码:
<phone:LongListSelector
x:Name="SouthLongListselector"
VerticalAlignment="Bottom"
ItemsSource="{Binding Cards}"
Canvas.ZIndex="{Binding Layer}"
SelectionChanged="SouthLongListselector_SelectionChanged"
LayoutMode="Grid"
GridCellSize="50,200"
Margin="0,0,0,-26"
>
<phone:LongListSelector.ItemTemplate >
<DataTemplate>
<Grid
Background="Transparent"
Margin="{Binding GridOffset}"
Height="150"
Width="110"
>
<!-- add image here-->
<Image
Source="{Binding Image}"
>
</Image>
</Grid>
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>
Run Code Online (Sandbox Code Playgroud)
提前致谢,
我正在尝试制作我自己的控件版本,比如 a TextBox,我想向其中添加一些自定义 UI。我想从这个类继承,这样用户仍然可以像调用普通文本框一样调用我的特殊文本框。我有以下几点:
// MySpecialTextBox.cs
public class MySpecialTextBox : TextBox
{
static MySpecialTextBox () { }
/* Some special properties */
}
Run Code Online (Sandbox Code Playgroud)
在 XAML 中:
<Style TargetType="{x:Type MySpecialTextBox}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type MySpecialTextBox}">
<StackPanel>
<Rectangle Width="100" Height="3" Fill="Pink" /> <!-- My own fancy lay-out -->
<TextBox Text="{TemplateBinding Text}"/> <!-- This text box should 'inherit' ALL the properties from the caller -->
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)
我的问题是,现在,TextBoxonly 获取您在此模板中明确设置的属性,在这种情况下,只有Text. 我想也结合Background,Foreground以及所有其他可能的属性。显然我可以做这样的事情: …
datatemplate ×10
wpf ×7
xaml ×3
.net ×2
mvvm ×2
binding ×1
c# ×1
canvas ×1
data-binding ×1
datatrigger ×1
hyperlink ×1
resources ×1
silverlight ×1
styles ×1
z-index ×1