Im trying to make dinamically loading url in a list Box by Binding.
Inicio.xaml.cs
void cardeek_DownloadUrlCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error != null) return;
textBox1.Text = e.Result;
XElement xmlUrl = XElement.Parse(e.Result);
listBox1.ItemsSource = from url in xmlUrl.Descendants("user")
select new TwitterItem { Url = url.Element("card").Element("url").Value, };
}
private void ContentPanel_Loaded(object sender, RoutedEventArgs e)
{
WebClient cardeekUrl = new WebClient();
cardeekUrl.DownloadStringCompleted += new DownloadStringCompletedEventHandler(cardeek_DownloadUrlCompleted);
cardeekUrl.DownloadStringAsync(new Uri("http://www.cardeek.com/wp7/response_url.php?email=" + "david.sonike#gmail.com" + "&code=" + "1"));
}
void cardeek_DownloadUrlCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error != null)
return;
textBox1.Text = e.Result;
XElement xmlUrl = XElement.Parse(e.Result);
listBox1.ItemsSource = from url in xmlUrl.Descendants("user")
select new TwitterItem
{
Url = url.Element("card").Element("url").Value,
};
}
private void ContentPanel_Loaded(object sender, RoutedEventArgs e)
{
WebClient cardeekUrl = new WebClient();
cardeekUrl.DownloadStringCompleted += new DownloadStringCompletedEventHandler(cardeek_DownloadUrlCompleted);
cardeekUrl.DownloadStringAsync(new Uri("http://www.cardeek.com/wp7/response_url.php?email=" + "david.sonike#gmail.com" + "&code=" + "1"));
}
Inicio.xaml
<ListBox Height="416" HorizontalAlignment="Left" Margin="41,191,0,0" Name="listBox1" VerticalAlignment="Top" Width="367">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Background="{Binding coItemBackground}">
<phone:WebBrowser HorizontalAlignment="Left" Margin="69,140,0,0" VerticalAlignment="Top" Height="121"
Width="137" Source="{Binding Url}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
and the Xml source in the web
www.marca.com
www.elmundo.com
www.vidaextra.com
In my Windows Phone 7 emulator cant see anything, anyone can help me solve the problem?¿
You are binding the Listbox to an Enumerable<TwitterItem>. As this doesn't implement INotifyPropertyChanged the UI isn't notified when you update the ItemSource in the callback.
As an alternative, consider creating an "ObservableCollection<TwitterItem> and binding to that. It automatically implements InotifyPropertyChanged so you don't need to.
You may also want to reconsider putting a WebBrowser inside the DataTemplate. There will be a performance cost of using it and it's unlikely to produce a good user experience.
Related
I am making a filter for a collection.
I know how to do it using CollectionViewSource.
But I wanted to do it without using CVS.
According to my ideas, there is a CollectionView in the ItemsControl.Items property and you can use the methods of this property.
The filter can be added without problems.
But after calling Items.Refresh() nothing changes.
Simple example:
<UniformGrid Columns="2">
<FrameworkElement.Resources>
<sc:StringCollection
x:Key="coll">
<sys:String>112</sys:String>
<sys:String>22</sys:String>
<sys:String>33</sys:String>
<sys:String>114</sys:String>
<sys:String>411</sys:String>
</sc:StringCollection>
<CollectionViewSource
x:Key="cvs"
Source="{Binding Mode=OneWay, Source={StaticResource coll}}"
Filter="OnFilterCV"/>
</FrameworkElement.Resources>
<TextBox x:Name="tBox"
Text="1"
TextChanged="OnTextChanged"
VerticalAlignment="Center"/>
<TextBox x:Name="tBoxCV"
Text="1"
TextChanged="OnTextChangedCV"
VerticalAlignment="Center"/>
<ItemsControl x:Name="iCtrl"
ItemsSource="{Binding Mode=OneWay, Source={StaticResource coll}}">
</ItemsControl>
<ItemsControl x:Name="iCtrlCV"
ItemsSource="{Binding Mode=OneWay, Source={StaticResource cvs}}">
</ItemsControl>
</UniformGrid>
public partial class MainWindow : Window
{
private readonly CollectionViewSource cvs;
public MainWindow()
{
InitializeComponent();
iCtrl.Items.Filter = OnFilter;
cvs = (CollectionViewSource)iCtrlCV.FindResource("cvs");
}
private bool OnFilter(object obj)
{
if (string.IsNullOrWhiteSpace(tBox.Text))
return true;
string item = (string)obj;
return item.Contains(tBox.Text, StringComparison.OrdinalIgnoreCase);
}
private void OnTextChanged(object sender, TextChangedEventArgs e)
{
Debug.WriteLine($"OnTextChanged:\"{tBox.Text}\"");
iCtrl?.Items.Refresh();
}
private void OnFilterCV(object sender, FilterEventArgs e)
{
e.Accepted = string.IsNullOrWhiteSpace(tBoxCV.Text) ||
((string)e.Item).Contains(tBoxCV.Text, StringComparison.OrdinalIgnoreCase);
}
private void OnTextChangedCV(object sender, TextChangedEventArgs e)
{
Debug.WriteLine($"OnTextChangedCV:\"{tBoxCV.Text}\"");
cvs?.View.Refresh();
}
}
Am I misunderstanding something or doing something wrong?
Updated.
Solution based on comment from #BionicCode.
private void OnTextChanged(object sender, TextChangedEventArgs e)
{
Debug.WriteLine($"OnTextChanged:\"{tBox.Text}\"");
//iCtrl?.Items.Refresh();
if (iCtrl != null)
iCtrl.Items.Filter = new Predicate<object>(OnFilter);
}
The ItemsControl.Items is of type ItemsCollection. ItemsCollection implements a different Refresh behavior. The Items property is basically intended for internal use. If you have to rely on CollectionView.Refresh you should use the CollectionView explicitly:
ItemsControl itemsControl;
itemsControl.Items.Filter = item => (item as string).Contains("A");
CollectionView collectionView = CollectionViewSource.GetDefaultView(itemsControl.ItemsSource);
collectionView.Refresh();
I have a tree view like this
<TreeView x:Name="tvFolders"
ItemsSource="{Binding TreeItems}"
Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2"
BorderBrush="{StaticResource ColligoBorderLightBrush}"
IsTextSearchCaseSensitive="False"
IsTextSearchEnabled="True"
VirtualizingPanel.VirtualizationMode="Recycling"
VirtualizingPanel.IsVirtualizing="True"
Loaded="tvFolders_Loaded">
</TreeView>
The binding TreeItems is an ObservableCollection.
If this tree is not very large, this works great but if I have many folders/subfolders structure it can take 10 seconds or so until it loads.
How do I solve the issue so tree is built faster?
Lazy loading can be done as mentioned below. Since it not good practice to post any links. I am posting links as well as code content in the link.
I got it from here. http://www.wpf-tutorial.com/treeview-control/lazy-loading-treeview-items/
<Grid>
<TreeView Name="trvStructure" TreeViewItem.Expanded="TreeViewItem_Expanded" Margin="10" />
</Grid>
public partial class LazyLoadingSample : Window
{
public LazyLoadingSample()
{
InitializeComponent();
DriveInfo[] drives = DriveInfo.GetDrives();
foreach(DriveInfo driveInfo in drives)
trvStructure.Items.Add(CreateTreeItem(driveInfo));
}
public void TreeViewItem_Expanded(object sender, RoutedEventArgs e)
{
TreeViewItem item = e.Source as TreeViewItem;
if((item.Items.Count == 1) && (item.Items[0] is string))
{
item.Items.Clear();
DirectoryInfo expandedDir = null;
if(item.Tag is DriveInfo)
expandedDir = (item.Tag as DriveInfo).RootDirectory;
if(item.Tag is DirectoryInfo)
expandedDir = (item.Tag as DirectoryInfo);
try
{
foreach(DirectoryInfo subDir in expandedDir.GetDirectories())
item.Items.Add(CreateTreeItem(subDir));
}
catch { }
}
}
private TreeViewItem CreateTreeItem(object o)
{
TreeViewItem item = new TreeViewItem();
item.Header = o.ToString();
item.Tag = o;
item.Items.Add("Loading...");
return item;
}
}
Here is a setup: I have a textbox with a numberic value. According to the requirements every time anybody changes that value an accompanying comment needs to be provided. So visually there must be another textbox for the comment that should be displayed right next to the first one. Ideally the comment textbox needs to be placed in a callout that originates from the value textbox and displayed on the right from it overlaying anything what's underneath of it just like on this picture:
I know how to do easily it in CSS and HTML.
I have to do the same in Silverlight now.
Unfortunately I am not very strong in it, so what I am specifically asking about is how having 2 textboxes make one of them appear next to another (on the right overlaying whatever controls are underneath it) with as less XAML and code as possible.
Use a ToolTip, and set the Placement such that it appears to the right. in XAML, you can template your ToolTip to look however you want, even if that means mimicking the TextBox appearance.
This is the purpose of the ToolTip, and I feel strongly that you should always use the right tool for the right job. :)
I hope this helps. Let us know if you need code samples.
EDIT: Added the following code samples:
<TextBox ToolTipService.Placement="Right">
<ToolTipService.ToolTip>
<TextBox Text="{Binding CalloutText, Mode=OneWay}" IsReadOnly="True"/>
</ToolTipService.ToolTip>
</TextBox>
Ok, I ended up writing my own behaviour
namespace MyNamespace
{
public class CommentBehavior : Behavior<TextBox>
{
private readonly TimeSpan howLongWeWaitBeforePopupCloses = TimeSpan.FromMilliseconds(200);
private DispatcherTimer popupClosingTimer;
public static DependencyProperty PopupProperty = DependencyProperty.Register("Popup", typeof(Popup), typeof(CommentBehavior), new PropertyMetadata(null));
public Popup Popup
{
get { return (Popup)this.GetValue(PopupProperty); }
set { this.SetValue(PopupProperty, value); }
}
protected override void OnAttached()
{
this.popupClosingTimer = new DispatcherTimer();
this.popupClosingTimer.Stop();
this.popupClosingTimer.Interval = howLongWeWaitBeforePopupCloses;
this.popupClosingTimer.Tick += this.ClosePopup;
this.AssociatedObject.GotFocus += this.GotFocus;
this.AssociatedObject.LostFocus += this.LostFocus;
this.Popup.Child.GotFocus += PopupChild_GotFocus;
this.Popup.Child.LostFocus += PopupChild_LostFocus;
}
private void PopupChild_LostFocus(object sender, RoutedEventArgs e)
{
this.popupClosingTimer.Start();
}
private void PopupChild_GotFocus(object sender, RoutedEventArgs e)
{
this.popupClosingTimer.Stop();
}
protected override void OnDetaching()
{
this.AssociatedObject.GotFocus -= this.GotFocus;
this.AssociatedObject.LostFocus -= this.LostFocus;
this.Popup.GotFocus -= PopupChild_GotFocus;
this.popupClosingTimer.Tick -= this.ClosePopup;
this.popupClosingTimer = null;
}
private void ClosePopup(object sender, EventArgs e)
{
this.Popup.IsOpen = false;
this.popupClosingTimer.Stop();
}
protected void GotFocus(object sender, RoutedEventArgs e)
{
this.popupClosingTimer.Stop();
this.Popup.IsOpen = true;
var at = this.CalculatePopupPosition();
this.Popup.HorizontalOffset = at.X;
this.Popup.VerticalOffset = at.Y;
}
private Point CalculatePopupPosition()
{
var owner = this.AssociatedObject;
var transformation = owner.TransformToVisual(Application.Current.RootVisual);
var at = transformation.Transform(new Point(owner.ActualWidth, 0));
return at;
}
protected void LostFocus(object sender, RoutedEventArgs e)
{
this.popupClosingTimer.Start();
}
}
}
And the following XAML
<Grid x:Name="LayoutRoot" Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Background="Red">
<TextBox Width="200" Text="0.01">
<i:Interaction.Behaviors>
<local:CommentBehavior>
<local:CommentBehavior.Popup>
<Popup>
<TextBox Text="Comment" />
</Popup>
</local:CommentBehavior.Popup>
</local:CommentBehavior>
</i:Interaction.Behaviors>
</TextBox>
</StackPanel>
</Grid>
I'm kinda confused with some problem, I'm doing a project where the user should be able to design questions with radio buttons, combo box, etc (kinda like toolbox from VS10 to design your XAML).
So far I can drag and drop an UIElement that I previously created, problem comes when the user creates a new element from my toolbox, I can't find the way to make that new UIElement to get the same events from my previosly created UIElement. Take a look at the code
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Canvas Height="190" HorizontalAlignment="Left" Margin="158,41,0,0" Name="canvas1" VerticalAlignment="Top" Width="322" AllowDrop="True">
<Button Content="PROBANDO" Height="23" Name="button" Width="75" Canvas.Left="113" Canvas.Top="43" PreviewMouseDown="button_PreviewMouseDown" PreviewMouseMove="button_PreviewMouseMove" MouseUp="button_MouseUp" IsEnabled="True" />
<TextBlock Canvas.Left="99" Canvas.Top="147" Height="23" Name="textBlock" Text="" Width="107" />
</Canvas>
<ListBox Height="190" Name="listBox" Width="126" Margin="12,41,365,80" >
<ListBoxItem Content="Radio Button" Selected="radio_Selected" Name="radio" />
<ListBoxItem Content="Text" Selected="text_Selected" Name="text" />
<ListBoxItem Content="Combo Box" Name="combo" Selected="combo_Selected" />
</ListBox>
</Grid>
</Window>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
Point p;
private void button_MouseUp(object sender, MouseButtonEventArgs e)
{
button.ReleaseMouseCapture();
}
private void button_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
button.CaptureMouse();
p = e.GetPosition(canvas1);
}
private void button_PreviewMouseMove(object sender, MouseEventArgs e)
{
Point x = e.GetPosition(canvas1);
if (e.LeftButton == MouseButtonState.Pressed)
{
Canvas.SetLeft(button, Canvas.GetLeft(button) + (x.X - p.X));
Canvas.SetTop(button, Canvas.GetTop(button) + (x.Y - p.Y));
}
p = x;
}
private void generic_PreviewMouseDown(UIElement sender, MouseEventArgs e)
{
Point x = e.GetPosition(canvas1);
if (e.LeftButton == MouseButtonState.Pressed)
{
Canvas.SetLeft(sender, Canvas.GetLeft(sender) + (x.X - p.X));
Canvas.SetTop(sender, Canvas.GetTop(sender) + (x.Y - p.Y));
}
p = x;
}
private void radio_Selected(object sender, RoutedEventArgs e)
{
RadioButton newRadio = new RadioButton();
canvas1.Children.Add(newRadio);
newRadio.PreviewMouseDown += generic_PreviewMouseDown(newRadio,?????);
textBlock.Text = listBox.SelectedIndex.ToString();
}
private void text_Selected(object sender, RoutedEventArgs e)
{
TextBox newText = new TextBox();
canvas1.Children.Add(newText);
textBlock.Text = (String)listBox.SelectedIndex.ToString();
}
private void combo_Selected(object sender, RoutedEventArgs e)
{
Console.Write("Combo");
textBlock.Text = (String)listBox.SelectedIndex.ToString();
}
}
Thanks!
If all you want to do is handle the mouse down on the new RadioButton, change this line:
newRadio.PreviewMouseDown += generic_PreviewMouseDown(newRadio,?????);
To this:
newRadio.PreviewMouseDown += generic_PreviewMouseDown;
Edit
And then you need to change the generic_PreviewMouseDown to the following:
private void generic_PreviewMouseDown(object sender, MouseEventArgs e)
{
UIElement elem = sender as UIElement;
Point x = e.GetPosition(canvas1);
if (e.LeftButton == MouseButtonState.Pressed)
{
Canvas.SetLeft(elem, Canvas.GetLeft(elem) + (x.X - p.X));
Canvas.SetTop(elem, Canvas.GetTop(elem) + (x.Y - p.Y));
}
p = x;
}
I am trying to create a slider(without binding).
Currently i did this:
Xaml:
<Slider Height="68" HorizontalAlignment="Left" Margin="52,45,0,0" x:Name="slider1" VerticalAlignment="Top" Width="256" Minimum="1" Maximum="40" Value="10" ValueChanged="slider1_ValueChanged" />
<TextBlock x:Name="textBlock1" Margin="52,120,0,0" Text="Slide it!" ></TextBlock>
And in my cs:
private void slider1_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e) {
textBloxk1.FontSize = slider1.Value;
}
But the silverlight page keeps loading and won't show the slider, anyone know what I'm doing wrong??
Probably at first ValueChanged event, slider1 and textblock1 are still null.
try this:
private void slider1_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
if (textBlock1 != null && slider1 != null)
{
textBlock1.FontSize = slider1.Value;
}
}
look at your Xaml.. you setting value to 10 Value="10"... but at that time textBlock dosn't exist.. be carefull..
when parser parse Xaml it first create Slider then sets all values to slider (and fire all attached events), and only then it creates TextBlock...
so change you code to this, and everithing should be fine..
private void slider1_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
if (textBlock1 != null && slider1 != null)
{
textBlock1.FontSize = slider1.Value;
}
}