|
I have basically just copied the approach found here[^]. I just made some minor adjustments and wrapped the functionality inside a CustomControl:
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
public class MyCanvas : Canvas
{
public static DependencyProperty ItemsSourceProperty =
DependencyProperty.Register("ItemsSource",
typeof(IEnumerable), typeof(MyCanvas),
new FrameworkPropertyMetadata((IEnumerable)null,
new PropertyChangedCallback(OnItemsSourcePropertyChanged)));
private static Dictionary<INotifyCollectionChanged, MyCanvas> references = new Dictionary<INotifyCollectionChanged, MyCanvas>();
public IEnumerable ItemsSource
{
get
{
return (IEnumerable)this.GetValue(ItemsSourceProperty);
}
set
{
this.SetValue(ItemsSourceProperty, value);
}
}
private static void OnItemsSourcePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var canvas = d as MyCanvas;
repopulateChildren(canvas);
if (canvas.ItemsSource is CompositeCollection)
{
CompositeCollection CollectionHolder = ((CompositeCollection)canvas.ItemsSource);
foreach (CollectionContainer item in CollectionHolder)
{
ObservableCollection<UIElement> elements = (ObservableCollection<UIElement>)item.Collection;
references.Add((elements as INotifyCollectionChanged), canvas);
elements.CollectionChanged -= collectionChangedHandler;
elements.CollectionChanged += collectionChangedHandler;
}
}
else if (canvas.ItemsSource is IEnumerable<UIElement>)
{
var be = BindingOperations.GetBindingExpression(canvas, MyCanvas.ItemsSourceProperty);
if (be != null)
{
var elements = (be.ResolvedSourcePropertyName == null ? be.ResolvedSource :
be.ResolvedSource.GetType().GetProperty(be.ResolvedSourcePropertyName).GetValue(be.ResolvedSource)) as INotifyCollectionChanged;
if (elements != null)
{
var cv = references.FirstOrDefault(i => i.Value == canvas);
if (!cv.Equals(default(KeyValuePair<INotifyCollectionChanged, MyCanvas>)))
references.Remove(cv.Key);
references[elements] = canvas;
elements.CollectionChanged -= collectionChangedHandler;
elements.CollectionChanged += collectionChangedHandler;
}
}
else references.Clear();
}
}
private static void collectionChangedHandler(object sender, NotifyCollectionChangedEventArgs e)
{
MyCanvas cv;
if (references.TryGetValue(sender as INotifyCollectionChanged, out cv))
{
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
foreach (var item in e.NewItems) cv.Children.Add(item as UIElement);
break;
case NotifyCollectionChangedAction.Remove:
{
foreach (var item in e.OldItems)
{
cv.Children.Remove(item as UIElement);
}
break;
}
case NotifyCollectionChangedAction.Reset:
repopulateChildren(cv);
break;
}
}
}
public static DependencyProperty PerservedChildrenProperty = DependencyProperty.Register("PerservedChildren", typeof(int), typeof(MyCanvas), new PropertyMetadata(0));
public int PerservedChildren
{
get
{
return (int)this.GetValue(PerservedChildrenProperty);
}
set
{
this.SetValue(PerservedChildrenProperty, value);
}
}
private static void repopulateChildren(MyCanvas cv)
{
for (int i = cv.Children.Count - 1; i >= cv.PerservedChildren; i--)
{
cv.Children.RemoveAt(i);
}
var elements = (IEnumerable)cv.GetValue(ItemsSourceProperty );
if (elements is CompositeCollection)
{
CompositeCollection CollectionHost = ((CompositeCollection)elements);
foreach (CollectionContainer item in CollectionHost)
{
foreach (UIElement pp in item.Collection)
{
if (!(cv.Children.Contains(pp)))
cv.Children.Add(pp);
}
}
}
else if (elements is IEnumerable<UIElement>)
{
foreach (UIElement elem in elements)
{
cv.Children.Add(elem);
}
}
}
}
I can now bind an ObservableCollection<uielement> directly to the ItemsSource and it will work properly.
The problem appeared when I wanted to create support binding a CompositeCollection to it as well. The problem seems to be that I can't find the appropriate instance of the MyCanvas in the Dictionary, as it will only delete points found in one of the CollectionContainers bounded to the CompositeCollection. What am I missing here?
I did try and look at the ItemControl posted at Reference Source[^] since it seems to have support for CollectionContainers out of the box. I do realize that I can use an ItemControl to bind items to a canvas as well WPF Canvas, how to add children dynamically with MVVM code behind - Stack Overflow[^], and that could solve my problem, but I want to know how to make my code work with CollectionContainers.
|
|
|
|
|
Well I did find a solution to my own question:
public class MyCanvas : Canvas
{
private static MyCanvas ThisCanvas;
public MyCanvas()
{
ThisCanvas = this;
}
...
}
Now I can call ThisCanvas from all static voids, but I'm curious as to how many instances will share this value?
|
|
|
|
|
Kenneth Haugland wrote: how many instances will share this value?
All of them. The value in a static field is shared across the whole AppDomain *.
Wouldn't it be simpler to get rid of the references dictionary, and make your collectionChangedHandler method an instance method?
Something like this should work:
private static void OnItemsSourcePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var canvas = (MyCanvas)d;
canvas.OnItemsSourcePropertyChanged(e);
}
protected virtual void OnItemsSourcePropertyChanged(DependencyPropertyChangedEventArgs e)
{
repopulateChildren(this);
var compositeCollection = e.OldValue As CompositeCollection;
if (compositeCollection != null)
{
foreach (CollectionContainer item in compositeCollection)
{
var elements = (ObservableCollection<UIElement>)item.Collection;
elements.CollectionChanged -= collectionChangedHandler;
}
}
else
{
var elements = e.OldValue as INotifyCollectionChanged;
if (elements != null)
{
elements.CollectionChanged -= collectionChangedHandler;
}
}
compositeCollection = e.NewValue As CompositeCollection;
if (compositeCollection != null)
{
foreach (CollectionContainer item in compositeCollection)
{
var elements = (ObservableCollection<UIElement>)item.Collection;
elements.CollectionChanged += collectionChangedHandler;
}
}
else
{
var elements = e.NewValue as INotifyCollectionChanged;
if (elements != null)
{
elements.CollectionChanged += collectionChangedHandler;
}
}
}
private void collectionChangedHandler(object sender, NotifyCollectionChangedEventArgs e)
{
...
}
* Unless the field is marked with the ThreadStatic attribute[^], in which case it's specific to a thread. But that wouldn't help you in this case.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Ah, yes, I didn't like the static value or Dictionary either, so I ended up using a delegate instead:
elements.CollectionChanged -= (sender, ee) => MyCollectionChangedHandler(cv, sender, ee);
private static void MyCollectionChangedHandler(MyCanvas cv, object sender, NotifyCollectionChangedEventArgs e)
I also thought they static members were shared everywhere in the application, but I made the attached property code below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace MyApplication
{
public static class TextBoxUpdateExplicitBinding
{
private static TextBox tb;
private static List<TextBox> MyList;
static TextBoxUpdateExplicitBinding()
{
MyList = new List<TextBox>();
}
#region "UpdateSourceOnKeyProperty"
public static readonly DependencyProperty UpdateSourceOnKeyProperty =
DependencyProperty.RegisterAttached(
"UpdateSourceOnKey",
typeof(Key),
typeof(TextBoxUpdateExplicitBinding),
new FrameworkPropertyMetadata(Key.None, KeyChanged));
public static void SetUpdateSourceOnKey(UIElement element, Key value)
{
element.SetValue(UpdateSourceOnKeyProperty, value);
}
public static Key GetUpdateSourceOnKey(UIElement element)
{
return (Key)element.GetValue(UpdateSourceOnKeyProperty);
}
private static void KeyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
tb = (TextBox)obj;
if (!MyList.Contains(tb))
{
MyList.Add(tb);
tb.PreviewKeyDown += TextBoxKeyDown;
}
}
#endregion
static void TextBoxKeyDown(object sender, KeyEventArgs e)
{
tb = (TextBox)sender;
if (tb == null) return;
var propertyValue = (Key)tb.GetValue(UpdateSourceOnKeyProperty);
if (e.Key != propertyValue) return;
var bindingExpression = tb.GetBindingExpression(TextBox.TextProperty);
if (bindingExpression != null) bindingExpression.UpdateSource();
}
public static DependencyProperty BindProperty = DependencyProperty
.RegisterAttached("Bind",
typeof(bool),
typeof(TextBoxUpdateExplicitBinding),
new PropertyMetadata(AttachCommand));
public static void SetBind(DependencyObject d, bool command)
{
d.SetValue(BindProperty, command);
}
private static void AttachCommand(DependencyObject d,
DependencyPropertyChangedEventArgs e)
{
(d as Button).Click += Button_Click;
}
private static void Button_Click(object sender, RoutedEventArgs e)
{
var bindingExpression = tb.GetBindingExpression(TextBox.TextProperty);
if (bindingExpression != null) bindingExpression.UpdateSource();
}
}
}
I now used this in a separate user contol:
<TextBox x:Name="tb" Grid.Column="0" local:TextBoxUpdateExplicitBinding.UpdateSourceOnKey="Return"
Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:SearchControl}},
Path=SearchText,
UpdateSourceTrigger=Explicit,
Mode=OneWayToSource }"/>
<Button Grid.Column="1" local:TextBoxUpdateExplicitBinding.Bind="True" >
<ContentControl>
<Image Source="pack://application:,,,/Icons/Search.png" Height="20" Width="20"/>
</ContentControl>
</Button>
And if I now added several of this user control in the same window, they didnt seem to share the static values in the attatched property. That's why I wondered.
|
|
|
|
|
Hey all, I have done UITests on Android, iOS, and Windows 10 apps, but now I have to develop and app that needs desktop support too. WPF seems to be the best way to support < Windows 10 devices to me, but I am interested if I Microsoft provides a library I can use to run UITests on a WPF application? Are there any libraries like this?
i cri evry tiem
|
|
|
|
|
|
Good Day
i have a code which is fired on the Button click even
Status.Background = new SolidColorBrush(Colors.Orange);
Status.Content = "Exporting, please wait...";
Logger.ExportLogToExcel();
Status.Background = new SolidColorBrush(Colors.Yellow);
Status.Content = "Exported to Excel Successfully";
What is Part 1 not working. Basically , i am Exporting to Excel , and i am changing the Label that shows the status, i am changing the color and also the contents. but the Dont change i only see "Exported to Excel Successfully"
I thought might be that the Export is quick to a point that it immediately jump to Success message. i added a thread to make to sleep for a few seconds before it can export so i see a different background.
Thanks
Vuyiswa Maseko,
Spoted in Daniweb-- Sorry to rant. I hate websites. They are just wierd. They don't behave like normal code.
C#/VB.NET/ASP.NET/SQL7/2000/2005/2008
http://www.vimalsoft.com
vuyiswa[at]vimalsoft.com
|
|
|
|
|
I am guessing that everything is done on the main thread so the UI thread locks until the method is completed and the last colour change is effected.
Unless you do the export on a separate thread you are not going to see the initial colour change. putting in a delay does not help, move the export to a BGW thread.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
i actually tried that initially. by setting the color from another Thread and the Export is obviously in another thread. but still the issue remains , let me keep trying something will post the feedback if i get to the solution.
Vuyiswa Maseko,
Spoted in Daniweb-- Sorry to rant. I hate websites. They are just wierd. They don't behave like normal code.
C#/VB.NET/ASP.NET/SQL7/2000/2005/2008
http://www.vimalsoft.com
vuyiswa[at]vimalsoft.com
|
|
|
|
|
No no no, you set the colour BEFORE you launch the BGW that exports the excel, in the BGW compleate method you set the colour the second time.
This allows the UI to refresh while the BGW is working.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
You are 100% Right, i was missing Something. its now working like charm.
Vuyiswa Maseko,
Spoted in Daniweb-- Sorry to rant. I hate websites. They are just wierd. They don't behave like normal code.
C#/VB.NET/ASP.NET/SQL7/2000/2005/2008
http://www.vimalsoft.com
vuyiswa[at]vimalsoft.com
|
|
|
|
|
- How do you unit test a piece of UI becoming visible/invisible? The bound bool may be correct, but the converter could fail.
- How do you unit test data validations?
If it's not broken, fix it until it is
|
|
|
|
|
Kevin Marois wrote: How do you unit test a piece of UI becoming visible/invisible? The bound bool may be correct, but the converter could fail. If you're that worried about the converter, you would write a UI automation test. However, a better bet would be to write a unit test for your converter to ensure it responds to changes (this is assuming you aren't using the standard visibility converter for some reason - but why would that one fail).
Kevin Marois wrote: How do you unit test data validations? Again, you test the underlying logic. If you are worried about UI behaviour, write UI automation tests.
This space for rent
|
|
|
|
|
Thanks.
I'm trying to write units tests to test acceptance criteria, and some of states "When this is true then this UI part should visible".
So it's not really about the converter as much as it is aligning tests with acceptance with the idea being that after any form of refactoring, the UI part is still respecting the condition.
If it's not broken, fix it until it is
|
|
|
|
|
In which case, a Coded Unit Test[^] is probably going to be your best bet.
This space for rent
|
|
|
|
|
Pete O'Hanlon wrote: (this is assuming you aren't using the standard visibility converter Where be this standard converter!
I have a bunch of converters that I call standard I did not know there are built in converters!
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
System.Windows.BooleanToVisibilityConverter. There are other converters that do things for you behind the scenes such as this[^] puppy.
This space for rent
|
|
|
|
|
Thank you, that gets rid of a folder of converters I have been using for ages. CP continues to be of value.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
You're welcome. I have to admit that I'm not the biggest fan of converters - they do tend to be misused a lot to by people trying to shoehorn functionality in that either belongs in a VM, or which can readily be done with triggers.
This space for rent
|
|
|
|
|
hi i am using wpf vs.net 2012 to do a simple optimization program for myself.
my goal is to load from a png file and be able to print the file to a a4 size paper after editing it a little(like adding my name).
at the moment i am using canvas control hold and print the image from.
i notice that this does not seems to be the best option. is there a more efficient way then using canvas?
|
|
|
|
|
Hello,
I'm looking for a simple XAML + C# code the does the following:
Upon application launch, loading 3 icons from png files into an image list.
On runtime (e.g every 1 sec) a new line is added to a ListView. Each row contains an icon + label.
The icon can be one out of the 3 previously loaded icons.
The XAML I currently have is (not sure it's OK):
<Window.Resources>
<sys:String x:Key="icon1">icon1.png</sys:String>
<sys:String x:Key="icon2">icon2.png</sys:String>
<sys:String x:Key="icon3">icon3.png</sys:String>
</Window.Resources>
...
<ListView x:Name="listView1" >
<ListView.ItemTemplate>
<DataTemplate>
<Image Source="{Binding}" Stretch="None" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Can you please send a C# code ?
Best regards,
Z.V
|
|
|
|
|
Sorry, but we don't write your code for you.
Why don't you try it yourself first, and then if you have a specific question, come back to ask it. If you do that, people will gladly help you.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
|
Hello,
I used the following XAML code:
<StackPanel x:Name="grd">
<ListView x:Name="listView">
<ListView.View>
<GridView>
<GridViewColumn Header="Severity" DisplayMemberBinding="{Binding Icon}"/>
<GridViewColumn Header="Id" DisplayMemberBinding="{Binding Id}"/>
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}"/>
<GridViewColumn Header="Immagine" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<Image Source="{Binding ImagePath}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</StackPanel>
</Window>
And the following C# code:
public partial class MainWindow : Window
{
public class MyItem
{
public string Icon { get; set; }
public int Id { get; set; }
public string Name { get; set; }
public string ImagePath { get; set; }
}
public MainWindow()
{
InitializeComponent();
MyItem Item = new MyItem();
Item.Icon = "1";
Item.Id = 0;
Item.Name = "Zvika";
Item.ImagePath = "icon1.png";
this.listView.Items.Add(Item);
}
}
I also added the file icon.png to the project.
Now it works.
Regards,
Z.V
modified 30-Dec-15 21:44pm.
|
|
|
|
|
I know that is an aberration but I am building a TreeListView where the Tree Column [0] is fixed and the following columns are dynamically created as a collection of columns in the viewmodel. The code behind then adds the columns to the TreeListView when they are built.
The creation of the columns is done on a BGW and a message is generated for the code behind to consume in the BGW complete method.
However the Message is complaining that it is on the wrong thread.
VM BGW Complete Method
private void NavComplete(object sender, RunWorkerCompletedEventArgs e)
{
worker.DoWork -= LoadTreeStuff; ;
worker.RunWorkerCompleted -= NavComplete; ;
if (e.Result != null)
{
MessageBox.Show(e.Result.ToString());
}
BusyIndicator = false;
AppMessages.MessageObject.Send("ColumnsReady");
}
I would have thought the AppMessage.MessageObject.Send would be on the main application thread.
public static class MessageObject
{
public static void Send(object oPayload)
{
Messenger.Default.Send<object>(oPayload);<-------Errors
}
<pre>
public static void Register(object recipient, Action<object> action)
{
Messenger.Default.Register<object>(recipient, action);
}
public static void UnRegister(object recipient)
{
Messenger.Default.Unregister<object>(recipient);
}
}</pre>
with the following error
Quote: The calling thread cannot access this object because a different thread owns it.
Any suggestions other than getting rid of the BGW (works perfection when it is removed)
Never underestimate the power of human stupidity
RAH
|
|
|
|