我有一个列表框,我有以下ItemTemplate:
<DataTemplate x:Key="ScenarioItemTemplate">
<Border Margin="5,0,5,0"
Background="#FF3C3B3B"
BorderBrush="#FF797878"
BorderThickness="2"
CornerRadius="5">
<DockPanel>
<DockPanel DockPanel.Dock="Top"
Margin="0,2,0,0">
<Button HorizontalAlignment="Left"
DockPanel.Dock="Left"
FontWeight="Heavy"
Foreground="White" />
<Label Content="{Binding Path=Name}"
DockPanel.Dock="Left"
FontWeight="Heavy"
Foreground="white" />
<Label HorizontalAlignment="Right"
Background="#FF3C3B3B"
Content="X"
DockPanel.Dock="Left"
FontWeight="Heavy"
Foreground="White" />
</DockPanel>
<ContentControl Name="designerContent"
Visibility="Collapsed"
MinHeight="100"
Margin="2,0,2,2"
Content="{Binding Path=DesignerInstance}"
Background="#FF999898">
</ContentControl>
</DockPanel>
</Border>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
如您所见,ContentControl的Visibility设置为折叠.
我需要定义一个触发器,使Visibility设置为"Visible"
当ListItem被选中时,我无法弄明白.
有任何想法吗?
更新:当然我可以简单地复制DataTemplate并向有问题的ListBox添加触发器以使用其中一个或另一个,但我想防止重复此代码.
我有一个ListBox
带ItemTemplate
组成的TextBlock
和ComboBox
.问题是TextBlock
每个项目内部文本的宽度不同,ComboBox
控件未对齐.
如何TextBlock
在模板中设置所有项目的宽度相同,哪个是最宽的?
这是我的xaml:
<ListBox MinHeight="100" ItemsSource="{Binding Trainees}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Margin="1">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock VerticalAlignment="Center" Grid.Column="0">
<TextBlock.Text>
<MultiBinding StringFormat="{}{0}, {1}">
<Binding Path="LastName" />
<Binding Path="FirstName" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
<ComboBox HorizontalAlignment="Left" Grid.Column="1"
ItemsSource="{Binding Source={StaticResource Functions}}" SelectedValue="{Binding Path=Function}"
MinWidth="100" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Run Code Online (Sandbox Code Playgroud) 在WPF中Listbox
,我很困惑与这些概念2:
ItemTemplate
和ItemContainerStyle
一个人能更给我解释一下?
我上了课FruitViewModel
.它描述了ListBox
项目的ViewModels .
<ListBox ItemsSource="{Binding Fruits}">
Run Code Online (Sandbox Code Playgroud)
而且我有
class BananaViewModel : FruitViewModel
Run Code Online (Sandbox Code Playgroud)
和
class AppleViewModel : FruitViewModel
Run Code Online (Sandbox Code Playgroud)
Fruits
包含绑定的BananaViewModel
s和AppleViewModel
s ItemsSource
.
如何为苹果和香蕉制作不同的模板?它们应该在一个列表中,但具有不同的模板
我有多个视频显示它们与Mainviewmodel中的视频选择绑定.一切正常,直到我尝试将enter命令绑定到Mainviewmodel.我不知道这个的语法.目前,绑定设置为Video而不是Mainviewmodel.
错误信息:
'StartVideoCommand' property not found on 'object' ''Video'
Run Code Online (Sandbox Code Playgroud)
XAML:
<Window.Resources>
<local:MainViewModel x:Key="MainViewModel"/>
</Window.Resources>
<Grid DataContext="{StaticResource MainViewModel}">
<ListBox ItemsSource="{Binding Videos}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.InputBindings>
!!! <KeyBinding Key="Enter" Command="{Binding StartVideo}" /> !Bound to Video not to Mainviewmodel grrr
</Grid.InputBindings>
... layout stuff
<TextBlock Text="{Binding Title}" Grid.Column="0" Grid.Row="0" Foreground="White"/>
<TextBlock Text="{Binding Date}" Grid.Column="0" Grid.Row="1" Foreground="White" HorizontalAlignment="Left"/>
<TextBlock Text="{Binding Length}" Grid.Column="1" Grid.Row="1" Foreground="White" HorizontalAlignment="Right"/>
... closing tags
Run Code Online (Sandbox Code Playgroud) 我试图了解如何最好地扩展ListBox
控件.作为一个学习的经验,我想建立一个ListBox
其ListBoxItem
场显示一个CheckBox
,而不仅仅是文字.我使用了基本方式工作ListBox.ItemTemplate
,明确设置我想要数据绑定的属性的名称.一个例子胜过千言万语,所以......
我有一个数据绑定的自定义对象:
public class MyDataItem {
public bool Checked { get; set; }
public string DisplayName { get; set; }
public MyDataItem(bool isChecked, string displayName) {
Checked = isChecked;
DisplayName = displayName;
}
}
Run Code Online (Sandbox Code Playgroud)
(我构建了一个列表并设置ListBox.ItemsSource
为该列表.)我的XAML看起来像这样:
<ListBox Name="listBox1">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Path=Checked}" Content="{Binding Path=DisplayName}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Run Code Online (Sandbox Code Playgroud)
这有效.但我想让这个模板可重用,即我想要绑定到除"Checked"和"DisplayName"以外的属性的其他对象.我如何修改我的模板,以便我可以使它成为一个资源,在多个ListBox
实例上重用它,并为每个实例绑定IsChecked
和Content
任意属性名称?
好吧,这是一个令人尴尬的简单问题,但让我发疯.我正在学习DataTemplating,并且我正在尝试将非常简单的ItemTemplate应用于ListBox.
但是,当我运行我的应用程序时,模板完全被忽略,我只是得到标准的列表框,而实际上我希望看到一个带有'Test'的复选框列表.
我已经尝试过几次并且总是一样的结果.我在Google上查了几个资源,并且在ListBox上都有相同的语法定义和ItemTemplate,所以我真的看不出我出错的地方.
码...
<Grid x:Name="LayoutRoot">
<ListBox x:Name="TestList"
SelectionMode="Multiple">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<CheckBox Content="Check this checkbox!"/>
<TextBlock>Test</TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.Items>
<ListBoxItem>Bob</ListBoxItem>
<ListBoxItem>Jim</ListBoxItem>
<ListBoxItem>Dave</ListBoxItem>
<ListBoxItem>Larry</ListBoxItem>
<ListBoxItem>Tom</ListBoxItem>
</ListBox.Items>
</ListBox>
</Grid>
Run Code Online (Sandbox Code Playgroud)
任何帮助非常感谢.抱歉这样一个看似愚蠢的问题,但我真的堕落在这里的第一个障碍:(
在
在WPF4.0中,我有一个类包含其他类类型作为属性(组合多个数据类型用于显示).就像是:
public partial class Owner
{
public string OwnerName { get; set; }
public int OwnerId { get; set; }
}
partial class ForDisplay
{
public Owner OwnerData { get; set; }
public int Credit { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
在我的窗口中,我有一个带有以下内容的ItemsControl(为了清晰起见而剪裁):
<ItemsControl ItemsSource={Binding}>
<ItemsControl.ItemTemplate>
<DataTemplate>
<local:MyDisplayControl
OwnerName={Binding OwnerData.OwnerName}
Credit={Binding Credit} />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)
然后我得到的数据层显示信息的集合,并设置DataContext
的ItemsControl
这个集合."Credit"属性正确显示,但OwnerName属性不正确.相反,我得到一个绑定错误:
错误40:BindingExpression路径错误:'对象'上找不到'OwnerName'属性''ForDisplay'(HashCode = 449124874)'.BindingExpression:路径= OWNERNAME; DataItem ='ForDisplay'(HashCode = 449124874); target元素是'TextBlock'(Name = txtOwnerName'); target属性是'Text'(类型'String')
我不明白为什么这会尝试在ForDisplay类中查找OwnerName属性,而不是在ForDisplay OwnerData属性的Owner类中查找.
编辑
它似乎与使用自定义控件有关.如果我将相同的属性绑定到a TextBlock
,它们可以正常工作.
<ItemsControl ItemsSource={Binding}>
<ItemsControl.ItemTemplate> …
Run Code Online (Sandbox Code Playgroud) 我在列表控件中有很多项.我希望每个项目都有不同的项目模板,具体取决于项目的类型.因此列表中的第一项是ObjectA类型,因此我希望它使用ItemTemplateA进行渲染.第二项是ObjectB类型,所以我希望它有ItemTemplateB进行渲染.目前我只能使用ItemTemplate设置为它们定义一个模板.有没有办法实现这个目标?
在WPF中Listbox
,我很困惑与这些概念2:
ItemTemplate
和ItemsPanelTemplate
一个人能更给我解释一下?
谢谢约翰
itemtemplate ×10
wpf ×10
listbox ×5
binding ×2
datatemplate ×2
.net-4.0 ×1
c# ×1
data-binding ×1
itemscontrol ×1
triggers ×1
viewmodel ×1
width ×1