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?
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical"></StackPanel>
</ItemsPanelTemplate>
Is being added to the the ItemsControl as an item instead of setting it as the ItemsPanel, this would be the right way:
<ItemsControl>
...
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
(This is redundant anyway, because the default panel is already a vertical StackPanel)
Try setting the ItemsSource on your items control
repeaterRecentProjects.ItemsSource = someFiles;
...snipped...
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:
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical"></StackPanel>
</ItemsPanelTemplate>
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.
Related
I have an Ellipse in my WPF application. I want to change the colour of its outline whenever it is double clicked. I found this (old) tutorial about making this work by using the available MouseDown event and checking for a ClickCount of two in the event handler. This is the simplest solution to my problem and I'd like to try and get this to work before creating an empty button Control Template.
However, I'm unable to find the clicked ellipse in my code behind file. Supposedly this works in the tutorial, but I'm wondering if I'm missing anything.
Here's the code that contains the ellipse. It is the 3rd column of a grid:
<ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" Grid.Column="3">
<StackPanel Orientation="Vertical" Margin="3,1" Background="GhostWhite">
<ItemsControl Name="FlowLinkItems" ItemsSource="{Binding FlowLinkList}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid Height="40">
<Ellipse Name="FlowLinkEllipse" Stroke="BlueViolet" Height="38" VerticalAlignment="Center" MouseDown="Ellipse_MouseDown"/>
<TextBlock TextAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center" Text="{Binding Message}"></TextBlock>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</ScrollViewer>
In the tutorial, the code behind method worked like this:
private void Ellipse_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ClickCount == 2)
{
FlowLinkEllipse.Stroke = "Red";
}
}
And the error I'm seeing is:
The name 'FlowLinkEllipse' does not exist in the current context
If this method is not possible I'm open to suggestions that are as simple as possible (I'm still new to WPF and the only thing my app will handle is this double click).
Note: I do have this line in my code behind and it works fine.
FlowLinkItems.MouseLeftButtonUp += FlowLinkItems_MouseLeftButtonUp;
As #Magus noted, you can't reference an item from code-behind, that is inside a DataTemplate. That should be no problem here, though: sender will contain a reference to the ellipse:
private void Ellipse_MouseDown(object sender, MouseButtonEventArgs e)
{
if (ellipse as sender == null || e.ClickCount < 2)
return;
var ellipse = (Ellipse)sender;
ellipse.Stroke = System.Windows.Media.Brushes.Red;
}
I'm quite new to C# and Windows Phone 7 for that sake, but none the less, I've thrown myself into trying to make a small app for myself. Here's my problem:
I'm trying to set up a DataTemplate that will position my Name and Drinks variables that I've declared in MainPage.xaml.cs. Here's my action when button1 is clicked:
private void button1_Click(object sender, RoutedEventArgs e)
{
string Name = participantName.Text;
int Drinks = 0;
listBox1.Items.Add(Name + Drinks);
}
And here is my DataTemplate from MainPage.xaml
<ListBox Height="Auto" HorizontalAlignment="Stretch" Margin="7,74,0,0" Name="listBox1" VerticalAlignment="Stretch" Width="Auto">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Height="132">
<TextBlock Text="{Binding Path=Name}" FontSize="35" />
<StackPanel Width="370">
<TextBlock Text="{Binding Path=Drinks}" FontSize="35" />
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
The problem is that my data is not shown. It works perfectly without the DataTemplate, but as soon as I use it, my text simply doesn't get through. Your help is very much appreciated.
The template itself is ok. The bindings on the template, though, are currently incorrect.
When you add a new item to the list box, you are just adding a plain old string (which is currently missing a space, BTW.) Your bindings, though, expect the object in the list to have a Name property and a Drinks property, which of course the string class does not have.
The usual solution here is to logically separate your data model from your presentation, by creating a class to store the data itself (probably PersonDrink, with the appropriate Name and Drinks properties) and then adding those objects to the list.
You should read up on the MVVM pattern, as it provides an excellent way to ensure that changes in your data are reflected in your view, and visa versa.
http://amarchandra.wordpress.com/2011/12/18/binding-multiple-object-in-wp7-using-listbox/
Here is a sample for binding data using a datatemplate. I hope this might help you.
I have a number of tags (string) in a JSON-formatted stream (resultFromServer) that I put into a list (articleTagList):
if (resultFromServer.tag != null)
{
for (int i = 0; i < resultFromServer.tag.Length; i++)
{
articleTagList.Add(resultFromServer.tag[i]);
}
listboxArticleTags.Items.Clear();
listboxArticleTags.ItemsSource = articleTagList;
}
The listboxArticleTags listbox is using the following data template:
<DataTemplate x:Key="myArticleTagsTemplate">
<HyperlinkButton x:Name="Tag" Content="{Binding Name}"/>
</DataTemplate>
The problem with this is that all tags/HyperlinkButtons end up on one line each:
[Code]
[Example]
[Silverlight]
I want them on a single row:
[Code] [Example] [Silverlight]
This is for a WP7 app which I'm sure limits my options but is this at all possible to do?
Thanks!
This works in regular SL and should also work on the phone. I would also consider switching to ItemsControl instead of ListBox because I don't think that you actually need support for selections in this case.
<ListBox>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"></StackPanel>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
I'm learning WPF, so I'm kind of n00b in this.
I saw some examples about how to do what I want to do, but nothing exactly...
The question: I want to bind List to ListBox. I want to do it in XAML, w/o coding in code behind. How can I achieve that?
Right now I do it that way:
XAML
<ListBox x:Name="FileList">
<ListBox.ItemTemplate>
<DataTemplate>
<Label Content="{Binding Path=.}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Code behind
public MainWindow()
{
// ...
files = new List<string>();
FileList.ItemsSource = files;
}
private void FolderBrowser_TextChanged(object sender, RoutedEventArgs e)
{
string folder = FolderBrowser.Text;
files.Clear();
files.AddRange(Directory.GetFiles(folder, "*.txt", SearchOption.AllDirectories));
FileList.Items.Refresh();
}
But I want to get rid of FileList.ItemsSource = files; and FileList.Items.Refresh(); in C# code.
Thanks
First, setup the binding in your listbox:
<ListBox x:Name="FileList" ItemsSource="{Binding Files}">
<ListBox.ItemTemplate>
<DataTemplate>
<Label Content="{Binding Path=.}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
or
<ListBox x:Name="FileList" ItemsSource="{Binding Files}" DisplayMemberPath="."/>
Next, make sure "Files" is a property in your DataContext (or code behind). (You can't bind to fields, only properties...)
Ideally, you'll want to make Files an ObservableCollection<T> instead of a List<T>, as well. This will allow the binding to handle adding or removing elements correctly.
If you do these two things, it should just work correctly.
Two tricks to add to Reed's answer:
1) If all you're displaying in your list box items is a string, you can avoid the ListBox.ItemTemplate folderol by just setting ListBox.DisplayMemberPath.
2) You can set the window's DataContext to itself. For instance, give the window a name of MyWindow and set its DataContext to {Binding ElementName=MyWindow}. Now you can bind to any of its public properties. (I'm pretty sure Reed's who I learned that trick from in the first place.)
we have an Array which is converted via a Binded Converter:
else if (TTools.IsOfBaseClass(value.GetType(), typeof(System.Activities.Presentation.Model.ModelItemCollection)))
{
OurBaseClass[] test = (value as ModelItemCollection).GetCurrentValue() as OurBaseClass[];
List<OurBaseClass> listOfArray = new List<OurBaseClass>();
foreach (OurBaseClass item in test)
{
listOfArray.Add(item);
}
return listOfArray;
}
the convertion works well but it is not shown in our dynamically gui
gui code with bindings:
<sap:WorkflowItemsPresenter xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:sap="clr-namespace:System.Activities.Presentation;assembly=System.Activities.Presentation" Grid.Column="0" Name="MyArray" Items="{Binding Path=ModelItem.MyArray}" MinWidth="150" Margin="0">
<sap:WorkflowItemsPresenter.SpacerTemplate >
<DataTemplate>
<TextBlock Foreground="DarkGray" Margin="30">..</TextBlock>
</DataTemplate>
</sap:WorkflowItemsPresenter.SpacerTemplate>
<sap:WorkflowItemsPresenter.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical" HorizontalAlignment="Center" Margin="0"/>
</ItemsPanelTemplate>
</sap:WorkflowItemsPresenter.ItemsPanel>
</sap:WorkflowItemsPresenter>
Why is the gui not shown as a List??? it works well without converter.
Thanks
Have you tried setting a breakpoint in the converter?
I think the first problem may be that ModelItem.MyArray is type ModelProperty, rather than ModelItemCollection.