场景:我有一个ListBox,ListBoxItems有一个DataTemplate.我想要做的是在DataTemplate中放置一个ContextMenu.问题是我希望这个ContextMenu ItemsSource根据窗口中的某些属性而不同.我最初的想法是,我可以将ItemsSource绑定到窗口中的Property,并返回ItemsSource; 但是,我似乎无法正确绑定此属性.我相信这是因为我在DataTemplate中,因此DataContext(我相信这是正确的词)是ListBoxItem而不是窗口.我怎样才能将DataTemplate中的ContextMenu绑定到DataTemplate之外的Property.
我有一个带有名为CanSeePhotos的布尔静态属性的类,这应该控制我的DataTemplate中图片的可见性.出于调试目的,我将"CanSeePhotos"绑定到DataTemplate中的文本块.
我想做的是:
我的问题是如果我在InitializeComponent()之后设置CanSeePhotos = true,那么数据仍然显示为CanSeePhotos为false(如果我在它工作正常之前这样做).这是为什么?如何修复它以便我可以在加载数据之前的任何时候设置值?
这是我如何绑定到我的DataTemplate中的静态变量:
<TextBlock Text="{Binding Source={x:Static DAL:LoggedInUser.CanSeePhotos}, Mode=OneWay}"/>
Run Code Online (Sandbox Code Playgroud)
这是LoggedInUser类:
public class LoggedInUser
{
public static bool CanSeePhotos { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
编辑:如果我将控件的可见性直接绑定到静态属性,它将根据属性的值显示/折叠:
Visibility="{Binding Source={x:Static DAL:LoggedInUser.CanSeePhotos}, Converter={StaticResource BooleanToVisibilityConverter}}"
Run Code Online (Sandbox Code Playgroud)
但我需要像这样使用DataTrigger:
<DataTrigger Binding="{Binding Source={x:Static DAL:LoggedInUser.CanSeePhotos}}" Value="true">
<Setter TargetName="icon" Property="Source" Value="{Binding Photo}"/>
</DataTrigger>
Run Code Online (Sandbox Code Playgroud)
在上面的情况下,如果属性为true,则setter永远不会被设置.
是什么赋予了?
WPF的新功能并且具有选项卡,并且在每个选项卡中,内容以弯曲的角落面板/窗口/ whateveryouwannacallit呈现.我不确定如何做到这一点(Style,ControlTemplate)但决定采用DataTemplate方式.
所以现在我有了这个DataTemplate:
<DataTemplate x:Key="TabContentPresenter" >
<Border Margin="10"
BorderBrush="{StaticResource DarkColorBrush}"
CornerRadius="8"
BorderThickness="2"
Grid.Row="0"
Padding="5"
Background="{TemplateBinding Background}">
<ContentPresenter Content="{Binding}" />
</Border>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
正如你可以看到的背景属性,我不想在内容中设置背景颜色,但不知道如何.我在这里使用它.
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="120"/>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<ContentControl ContentTemplate="{StaticResource TabContentPresenter}" Background="White">
<!-- Something Here -->
</ContentControl>
<ContentControl ContentTemplate="{StaticResource TabContentPresenter}" Grid.Row="1" Background="Blue">
<!-- Something Here -->
</ContentControl>
</Grid>
Run Code Online (Sandbox Code Playgroud)
在这里使用DataTemplate是错误还是有其他方法吗?
我可以直接在内容上设置背景,并从模板中的填充更改为内容中的边距,但在一些类似的情况下无法工作,只需设置一次就更好了.
编辑:
根据建议,我改为ControlTemplate并将其置于样式中.这解决了背景问题,但创造了一个更大的问题.现在内容不会出现.我在博客上看到,放一个targetType解决了这个问题,但它并没有解决我的问题.代码现在看起来像这样,并且还改变了ContentControl以使用样式而不是模板.
<Style x:Key="TabContentPresenter" TargetType="ContentControl" >
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ContentControl">
<Border Margin="10"
BorderBrush="{StaticResource DarkColorBrush}"
CornerRadius="8"
BorderThickness="2"
Grid.Row="0"
Background="{TemplateBinding Background}">
<ContentPresenter Content="{Binding}" />
</Border>
</ControlTemplate>
</Setter.Value> …Run Code Online (Sandbox Code Playgroud) 我有一个ListBox,其ItemsSource绑定到一个对象列表.Listbox有一个ItemTemplate,其中包含一个包含TextBlock的DataTemplate.文本块的Text绑定到对象的Name属性(即Text ="{Binding Name}").
我想提供一个单选按钮来显示同一列表的不同视图.例如,允许用户在Name属性和ID属性之间切换.
我在2381740找到了一个SO答案,但我也在数据模板中设置了边框和文本框样式(参见下面的代码).
反正只是重置Textblock绑定?我不想重新创建整个datatemplate.实际上我甚至不确定如何做到这一点,是否有一种简单的方法将xaml转换为代码?
谢谢科迪
<DataTemplate>
<Border Margin="0 0 2 2"
BorderBrush="Black"
BorderThickness="3"
CornerRadius="4"
Padding="3">
<TextBlock Style="{StaticResource listBoxItemStyle}"
Text="{Binding Name}" />
</Border>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud) 我正在尝试在代码隐藏中创建DataTemplate.我的DataTrigger存在问题.
这是DataTemplate,用xaml编写:
<DataTemplate x:Key="XamlTemplate" >
<TextBox Text="{Binding Name}" Name="element" Width="100"/>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Flag}" Value="true">
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="element" Storyboard.TargetProperty="Width"
To="200" Duration="0:0:2" />
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
这是我用c#写的
var template = new DataTemplate();
//create visual tree
var textFactory = new FrameworkElementFactory(typeof(TextBox));
textFactory.SetBinding(TextBox.TextProperty, new Binding("Name"));
textFactory.SetValue(TextBox.NameProperty, "element");
textFactory.SetValue(TextBox.WidthProperty, 100D);
template.VisualTree = textFactory;
//create trigger
var animation = new DoubleAnimation();
animation.To = 200;
animation.Duration = TimeSpan.FromSeconds(2);
Storyboard.SetTargetProperty(animation, new PropertyPath("Width"));
Storyboard.SetTargetName(animation, "element");
var storyboard = new …Run Code Online (Sandbox Code Playgroud) 我有10个ComboBox控件,它们将使用相同的项目模板(图像和文本块)以及相同的项目,因此我想在更全局的范围内(页面级别)定义此模板.这是我到目前为止所做的:
<UserControl.Resources>
<DataTemplate x:Name="CBItem">
<StackPanel Orientation="Horizontal">
<Image Source="{Binding ImageSource}"></Image>
<TextBlock Text="{Binding TextLabel}"></TextBlock>
</StackPanel>
</DataTemplate>
</UserControl.Resources>
Run Code Online (Sandbox Code Playgroud)
问题是我不知道如何在以下10个ComboBox控件中使用此资源.我尝试过类似的东西
<ComboBox Height="25">
<ComboBox.ItemTemplate>
<DataTemplate x:Name="{StaticResource CBItem}"></DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
Run Code Online (Sandbox Code Playgroud)
但它不起作用.有帮助吗?
<Grid>
<ItemsControl ItemsSource="{Binding ScreenViewModelCollection}">
<ItemsControl.Resources>
<DataTemplate x:Key="SomeKey" TargetType="{x:Type local:RedScreenObject}">
<local:RedScreenObject/>
</DataTemplate>
</ItemsControl.Resources>
</ItemsControl>
</Grid>
Run Code Online (Sandbox Code Playgroud)
我不知道它为什么看不到TargetType属性.应该在Resources或ItemTemplate下?我试过它两种方式都找不到它.
编辑:为了被更多的downvotes轰炸ha ha它是参考这个:http: //chat.stackoverflow.com/transcript/message/4295316#4295316
在创建资源时,我们在其中指定DataType:
<Window.Resources>
<DataTemplate x:Key="StudentView"
DataType="this:StudentData">
<TextBox Text="{Binding Path=StudentFirstName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Grid.Row="1"
Grid.Column="2"
VerticalAlignment="Center" />
<TextBox Text="{Binding Path=StudentGradePointAverage}"
Grid.Row="2"
Grid.Column="2"
VerticalAlignment="Center" />
</DataTemplate>
<Window.Resources>
Run Code Online (Sandbox Code Playgroud)
虽然绑定:
<ItemsControl ItemsSource="{Binding TheStudents}"
ItemTemplate="{StaticResource StudentView}">
Run Code Online (Sandbox Code Playgroud)
那么为什么我们使用DataType,即使我删除了DatType,我的样本运行正常.是否限制某些类型,可以在DataTemplete中?
但我尝试将其中一个TextBox与垃圾值绑定(View-Model中不存在),它运行正常!
美好的一天,
我有一个通过CollectionViewSource填充的组合框。通过传入项目类型的数据模板(在本例中为ProjectViewModel)构建项目。这是.NET 4.0中的WPF。
在我的window.resources中,我指定了以下内容:
<Style TargetType="{x:Type ComboBoxItem}">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
</Style>
Run Code Online (Sandbox Code Playgroud)
尽管有这种风格,我仍然遇到以下错误:
System.Windows.Data错误:4:找不到参考'RelativeSource FindAncestor,AncestorType ='System.Windows.Controls.ItemsControl',AncestorLevel ='1''的绑定源。BindingExpression:Path = HorizontalContentAlignment; DataItem = null; 目标元素是'ComboBoxItem'(Name =''); 目标属性为“ HorizontalContentAlignment”(类型为“ HorizontalAlignment”)
System.Windows.Data错误:4:找不到参考'RelativeSource FindAncestor,AncestorType ='System.Windows.Controls.ItemsControl',AncestorLevel ='1''的绑定源。BindingExpression:Path = VerticalContentAlignment; DataItem = null; 目标元素是'ComboBoxItem'(Name =''); 目标属性为“ VerticalContentAlignment”(类型为“ VerticalAlignment”)
我也对ComboBox元素指定了水平和垂直ContentAlignment,但无济于事。这不是一个严重的问题,因为项目正确显示。但是,在调试时,关闭窗口时确实会延迟约10秒钟,同时它将大约4000条错误消息输出到输出窗口(我需要打开它才能捕获合法的绑定错误。
我可能无法正确读取错误。为什么找不到绑定的有效来源?据我所知,我使用ComboBox和CollectionViewSource的方式符合他们的意图。
我想在WPF中使用TreeViews可视化以下数据结构:
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;}
}
Run Code Online (Sandbox Code Playgroud)
这是我到目前为止的尝试:
<!-- 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=Actions}" >
<DataTemplate DataType="{x:Type myns:Inner}">
<Button Command={Binding Path=OnClick}>
<TextBlock Text="{Binding Path=Description}"/>
</Button>
</DataTemplate>
</TreeView>
</StackPanel>
</DataTemplate>
</TreeView.Resources>
</TreeView>
Run Code Online (Sandbox Code Playgroud)
由于我得到以下信息,因此访问此类访问有问题InvalidOperationException:
Operation is not valid while ItemsSource is …Run Code Online (Sandbox Code Playgroud) datatemplate ×10
wpf ×9
xaml ×3
binding ×2
c# ×2
combobox ×2
data-binding ×2
.net-3.5 ×1
code-behind ×1
datatrigger ×1
itemtemplate ×1
properties ×1
silverlight ×1
textblock ×1
treeview ×1