我有以下用户控件:
<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 …
如何选择一个不同的数据模板来匹配数据绑定源中的字段?我会有2个不同的DataTemplates
<DataTemplate x:Key="DataTemplateLabel">
<Label Width="60" Height="25" Background="Red">
<TextBlock Text="{Binding Path=Name}"/>
</Label>
</DataTemplate>
<DataTemplate x:Key="DataTemplateTxtBox">
<TextBox Width="60" Height="25" Background="Red" Text="{Binding Path=Text}"/>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
if(isText)然后使用DataTemplateTxtBox ELSE使用DataTemplateLabel)这可能吗?谢谢.
我有以下内容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中仍然无法开箱即用,我很高兴知道是否有人提出了一些合理的替代方案.
谢谢.
我编写了一个片段来通过c#代码创建自己的DataTemplate.然后我将它添加到datagrid列的编辑模板中.当我调用时object templateContent = tc.CellTemplate.LoadContent ( );,应用程序崩溃,并抛出一个异常,"FrameworkElementFactory必须在此操作的密封模板中.".这是我创建datatemplate的代码.
public override DataTemplate GenerateCellTemplate ( string propertyName )
{
DataTemplate template = new DataTemplate ( );
var textBlockName = string.Format ( "{0}_TextBlock", propertyName );
FrameworkElementFactory textBoxElement = new FrameworkElementFactory ( typeof ( TextBlock ), textBlockName );
textBoxElement.SetBinding ( TextBlock.TextProperty, new Binding ( propertyName ) );
template.VisualTree = textBoxElement;
Trigger trigger = new Trigger ( );
return template;
}
Run Code Online (Sandbox Code Playgroud) wpf exception datatemplate wpfdatagrid frameworkelementfactory
当人们无法使用Expression Blend时,人们如何找到.NET控件的默认模板?
到目前为止,当我需要WPF控件的模板时,我访问了Codeplex上的WPF项目页面,浏览了源代码,并且通常在"Generic.xaml"文件中找到了默认模板.
但现在我正在寻找"Frame"控件的默认模板.我在Codeplex上找不到它,我也没有在MS提供的.NET框架源中看到它(没有提供XAML文件,只有.cs文件).
Style Snooper工具提供了一个默认模板,但它似乎是逆向工程(不必要的冗长和引用内部类)而不是原始的清晰定义.
那么,穷人如何获得这些模板呢?
编辑:当使用标准.NET ResourceDictionary时也会出现此问题,并且似乎是在控件或数据模板中使用资源字典的问题.
我有一个自定义资源字典遵循共享资源实例的常见方法.
http://softnotes.wordpress.com/2011/04/05/shared-resourcedictionary-for-silverlight/ http://www.wpftutorial.net/MergedDictionaryPerformance.html
public class SharedResourceDictionary : ResourceDictionary
{
static readonly Dictionary<Uri, WeakReference<ResourceDictionary>> SharedDictionaries = new Dictionary<Uri, WeakReference<ResourceDictionary>>();
Uri _sourceUri;
public new Uri Source
{
get
{
// Behave like standard resource dictionary for IDE...
if (VisualStudio.IsInDesignMode)
return base.Source;
return this._sourceUri;
}
set
{
// Behave like standard resource dictionary for IDE...
if (VisualStudio.IsInDesignMode)
{
base.Source = value;
return;
}
this._sourceUri = value;
WeakReference<ResourceDictionary> cached;
if (SharedDictionaries.TryGetValue(value, out cached))
{
ResourceDictionary rd;
if (cached.TryGetTarget(out rd))
{
this.MergedDictionaries.Add(rd);
return; …Run Code Online (Sandbox Code Playgroud) 在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)
提前致谢,
我的项目有一个带有3个DataTemplates的ToolBar:
<ToolBar ItemsSource="{Binding ContextActions}" Background="Transparent" ToolBarTray.IsLocked="True">
<ToolBar.Resources>
<DataTemplate DataType="{x:Type viewModels:SimpleContextActionViewModel}">
<Button Command="{Binding ActionCommand}" Style="{StaticResource ToolBarButtonStyle}" ToolTip="{userInterface:Translation Binding={Binding ToolTip}}">
<ContentControl Template="{Binding Icon,Converter={StaticResource NameToResourceConverter}}" Margin="5" />
</Button>
</DataTemplate>
<DataTemplate DataType="{x:Type viewModels:SeparatorViewModel}">
<Rectangle Fill="{StaticResource SeparatorBrush}" Width="1" VerticalAlignment="Stretch" Margin="2,7" />
</DataTemplate>
<DataTemplate DataType="{x:Type viewModels:PopupContextActionViewModel}">
<Grid>
<ToggleButton IsChecked="{Binding ElementName=ContextActionPopup, Mode=TwoWay,Path=IsOpen}" Style="{StaticResource ToolBarButtonStyle}"
ToolTip="{userInterface:Translation Binding={Binding ToolTip}}">
<ContentControl Template="{Binding Icon, Converter={StaticResource NameToResourceConverter}}" Margin="5" />
</ToggleButton>
<Popup Name="ContextActionPopup" Height="150" Width="150" StaysOpen="False">
<Border BorderBrush="{StaticResource PopupBorderBrush}" BorderThickness="1" Background="White">
<ContentControl userInterface:RegionHelper.RegionName="{Binding RegionId}" />
</Border>
</Popup>
</Grid>
</DataTemplate>
</ToolBar.Resources>
</ToolBar>
Run Code Online (Sandbox Code Playgroud)
ItemsSource是一个ObservableCollection <object> …
datatemplate ×10
wpf ×7
.net ×2
binding ×2
c# ×2
mvvm ×2
xaml ×2
canvas ×1
exception ×1
resources ×1
silverlight ×1
wpfdatagrid ×1
z-index ×1