How do I put multiple HyperlinkButtons on a row in Silverlight? - c#

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>

Related

WPF color matrix

I have created a Class called matrix (Matrix [,] matrix;) and there I have the i and j attributes, which refer to the corresponding row and column.
Let's say, for example, that the [1,6] cell contains the number 6 (the content does not really matter as it is an example), or I want to represent it with a red color since 6 is greater than 5.
What I was wondering is what is the best and easiest way to represent this matrix in a Grid (Grid, Datagrid, ...) and for example change the [1,6] element of this grid to the red color, displaying it as the original matrix. This is an example I had but it dows not work, in fact it does not even create a Grid:
System.Data.DataTable dt = new System.Data.DataTable();
...loop for dt...
dt.Columns.Add();
dt.Rows.Add();
datagrid1.DataSource = dt;
Thanks in advance!
This is just one of many possible solutions:
<ItemsControl ItemsSource="{Binding SomeMatrix}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid IsItemsHost="True" Columns="{Binding SomeMatrix.Columns}" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Label Content="{Binding}" HorizontalContentAlignment="Center" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
U can then change the appearance of individual cells by modyfying the item template. The only prerequisite is that your Matrix class should implement IEnumerable interface and should yield items in a column-by-column, row-by-row order.

Windows phone proper way to display collection of items

I have XML file with my items and I'm deserializing them into ObservableCollection<UserControl> and I want to put it to my ItemsControl with my own layout.
Now I've created my own UserControl to handle deserialized data to layout and ObservableCollection is binded to my ItemsControl but if I want to display more than 5-10 items my app if getting unresponsive for a while.
How can I avoid freezes? Should I use DataTemplate within ItemsControl or any other ideas? I'm wondering how its done in apps like twitter or reddit which have many entries and everything is working quite nice. Have already searched for reddit/twitter app source to look how they have implemented it, but without success.
EDIT:
xaml
<ScrollViewer>
<ItemsControl x:Name="items" ItemsSource="{Binding}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</ScrollViewer>
c#
public MainPage()
{
this.InitializeComponent();
items.DataContext = _items;
}
public ObservableCollection<my_item> _items = new ObservableCollection<my_item>();
adding items
for (int i = 0; i < 40; i++)
{
_items.Add(new my_item());
}
my_item is my own UserControl with 2 small images, few buttons and a single TextBlock.
You should use ListView which has a virtualizing panel for displaying items by default:
<ListView ItemsSource={Binding Items} >
<ListView.ItemTemplate>
<DataTemplate>
<!-- here goes your item layout -->
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Maybe one reason for your app "hanging" is if you are deserializing/parsing your XML file on the UI thread, especially if the XML file is large, so the solution to this problem is that you need to do the XML parsing on a background thread:
Task.Run(() =>
{
var items = ParseXML();
});
After edit:
1)
Don't wrap an ItemsControl inside a ScrollViewer, because you will break the virtualization. The ItemsControl itself has its own ScrollViewer so there is no need of an additional one.
If you are on Windows Phone 8.1 Runtime, use ItemsStackPanel instead of VirtualizingStackPanel.
2)
It is recommended for lists with many items to use DataTemplates instead of filling it with UI elements. So rather create a data template from your user control:
<DataTemplate>
<MyUserControl/>
</DataTemplate>

WPF Noob Seeking Guidance

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.

Searching for a Best DataTemplate knowledge in WPF and the Properties used in it?

I want brief knowledge about Data Template for Customizing a control(like Combo Box,List Box etc.)in WPF using C#.NET. So if anybody have any links or sample applications then share it with me please..
Update:
I got to know the DataTemplate in some what but now I want to know about the terms used for DataTemplate like ObservableCollection,DataContext and how to set the Binding property according to the User's need. I want an idea for develop a very similar kind of Sample application like dividing Combo Box's each items into three Column and adding different contents on different columns dynamically
Thanks in Advance
here is it used very simply - but basically a DataTemplate allows you to represent data using XAML
<ItemsControl ItemsSource="{Binding Path=SomeDataCollection}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=SomeProperty}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
You should check out the WPF Quiz demo : http://community.infragistics.com/pixel8/media/p/91950.aspx It will teach you MVVM and the power of DataTemplates in one go :)
Suppose you want to show the button in each item of ComboBox , so you can do this by overriding its ItemTemplate method
<ComboBox>
<ComboBox.ItemTemplate>
<DataTemplate>
<Button Content="Sa"></Button>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
and in the code behind
List<string> lst = new List<string>();
for (int i = 0; i < 5; i++)
{
lst.Add("Sa" + i.ToString());
}
cmb.ItemsSource = lst;
so now when you run this , you will get the desired output , each combo item will be a button

WPF ListBox not updating with the ItemsSource

I have what I believe should be simple two-way databinding in WPF setup, but the listbox (target) is not updating as the collection changes.
I'm setting this ItemsSource of the ListBox programmatically:
lstVariable_Selected.ItemsSource = m_VariableList;
And the ListBox is declared as follows:
<ListBox Margin="5" Name="lstVariable_Selected">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderBrush="Gray" BorderThickness="1" Margin="0">
<TextBlock FontSize="25" Text="{Binding Path=Name}" />
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
When I initially set the ItemsSource, the ListBox (which is not visible at the time) gets its items set. However, if I go view the ListBox, updates seem to stop at that point.
I can then remove an item from the m_VariableList collection, and it does not disappear from the ListBox. Likewise, if I add one, it doesn't appear.
What gives?
Is your m_VariableList implementing INotifyCollectionChanged? If it's not an ObservableCollection, then changes to it's contents will not automatically be reflected in the UI.
The problem is not in the XAML that you have provided. I used the same XAML successfully in a test application; however, I was able to replicate the issue you are experiencing by re-instantiating the m_VariableList variable.
When the m_VariableList is given a new instance, or pointed to a new object, it is not reflected in the ListBox because the control has its own reference to the data. This may not be the cause of your problem, but I'd recommend looking over your code-behind to ensure that the variable is not getting re-instantiated.
i got stuck for more than hour and then simple logic solved this problem
just set itemsource to clear list and then set source u need again
lstVariable_Selected.ItemsSource = new List<Object>();
lstVariable_Selected.ItemsSource = m_VariableList;

Categories