WPF菜鸟寻求指导
所以,我大部分时间都生活在 ASP.NET MVC Land - 然而,现在我正在尝试使用 WPF 为我们的解决方案制作一些分析工具(又名,它会计算代码行数、哪些文件有测试、提取元数据)来自属性的数据等)。一切都进展顺利(我的反射代码工作正常)。我遇到的问题是 WPF ItemsControl。与传统的
相比,ItemsControl 完全令我困惑。
我的 XAML:
<ItemsControl x:Name="repeaterRecentProjects">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border Background="#094563" Margin="5" Padding="3">
<DockPanel>
<Image Source="/Content/Black-Internal-icon.png" Height="16" Width="16"></Image>
<TextBlock Margin="5,0" Text="{Binding}"></TextBlock>
</DockPanel>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical"></StackPanel>
</ItemsPanelTemplate>
</ItemsControl>
据我了解,这个想法是 DataTemplate 包含我的模板。然后,在代码中我可以执行类似的操作(编辑:页面后面的完整代码):
public partial class HomeScreen : Page
{
protected bool _isProjectChosen = false;
public HomeScreen()
{
InitializeComponent();
}
protected ObservableCollection<string> someFiles = new ObservableCollection<string>();
protected override void OnInitialized(EventArgs e)
{
base.OnInitialized(e);
someFiles.Add("SomeFile.aspx");
someFiles.Add("SomeFile2.aspx");
someFiles.Add("SomeFile3.aspx");
repeaterRecentProjects.ItemsSource = someFiles;
}
private void buttonSelectProject_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog dialogSelectProject = new OpenFileDialog();
dialogSelectProject.DefaultExt = ".sln";
dialogSelectProject.Filter = "Visual Studio Solution (.sln)|*.sln";
if (dialogSelectProject.ShowDialog() == true)
{
textBlockProjectName.Text = dialogSelectProject.FileName;
_isProjectChosen = true;
buttonAnalyzeProject.IsEnabled = true;
}
someFiles.Add("AnotherString.aspx");
}
private void buttonAnalyzeProject_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Click");
}
}
但是,每当我运行应用程序时,我在项目控件中看不到三个项目,只有一个项目。这是为什么呢?另外,我的绑定表达式是否正确?由于我没有绑定到字符串的属性,因此简单的 {Binding}
应该可以接受,对吧?
So, most of the time I live in ASP.NET MVC Land - however, right now I'm trying to make some analysis tools for our solution using WPF (aka, it would count lines of code, which files have tests, extracting meta data from attributes, etc). Everything is mostly going well (the reflection code I have works fine). What I'm having trouble with is the WPF ItemsControl. Compared to a tradition <asp:Repeater />
, ItemsControl is completely boggling me.
My XAML:
<ItemsControl x:Name="repeaterRecentProjects">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border Background="#094563" Margin="5" Padding="3">
<DockPanel>
<Image Source="/Content/Black-Internal-icon.png" Height="16" Width="16"></Image>
<TextBlock Margin="5,0" Text="{Binding}"></TextBlock>
</DockPanel>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical"></StackPanel>
</ItemsPanelTemplate>
</ItemsControl>
The idea is that the DataTemplate contains my template, as I understand it. Then, in code I can do something like this (EDIT: Full code behind of the page):
public partial class HomeScreen : Page
{
protected bool _isProjectChosen = false;
public HomeScreen()
{
InitializeComponent();
}
protected ObservableCollection<string> someFiles = new ObservableCollection<string>();
protected override void OnInitialized(EventArgs e)
{
base.OnInitialized(e);
someFiles.Add("SomeFile.aspx");
someFiles.Add("SomeFile2.aspx");
someFiles.Add("SomeFile3.aspx");
repeaterRecentProjects.ItemsSource = someFiles;
}
private void buttonSelectProject_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog dialogSelectProject = new OpenFileDialog();
dialogSelectProject.DefaultExt = ".sln";
dialogSelectProject.Filter = "Visual Studio Solution (.sln)|*.sln";
if (dialogSelectProject.ShowDialog() == true)
{
textBlockProjectName.Text = dialogSelectProject.FileName;
_isProjectChosen = true;
buttonAnalyzeProject.IsEnabled = true;
}
someFiles.Add("AnotherString.aspx");
}
private void buttonAnalyzeProject_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Click");
}
}
However, whenever I run my application, I don't see three items in my items control, only one item. Why is this? Also, would my binding expression be correct? Since I'm not binding to a property of string, simply {Binding}
should be acceptable right?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正在将其作为项目添加到 ItemsControl,而不是将其设置为
ItemsPanel
,这将是正确的方式:(这无论如何都是多余的,因为默认面板已经是垂直 StackPanel )
Is being added to the the ItemsControl as an item instead of setting it as the
ItemsPanel
, this would be the right way:(This is redundant anyway, because the default panel is already a vertical StackPanel)
尝试在项目控件上设置 ItemsSource
Try setting the ItemsSource on your items control
我已经找到了问题所在,但可以使用一些指导来了解问题发生的原因。当我从 XAML 中删除以下节点时,它可以正常工作:
为什么要在项目集合中计算该模板?我的理解是,这只是它选择添加每个数据模板的容器项。
I've figured out the problem, but could use some guidance on why it was occuring. When I removed the following nodes from the XAML, it works correctly:
Why was it counting that template among the items collection? My understanding was that this was simply the container item it chose to add each data template to.