Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Unfortunately this modified response is not as elegant as I'd like, but it should work.</p> <p>Basically, use another dummy control as a relay for the binding. Specifically, this scenario arises when you want to pull the first and last of a collection. You can't just use a converter to grab the first/last and the property all at once, because if you change a property on the first or last item, the converter won't pick up the change. So you have to do something that melds in with dependency properties - almost like some sort of a second order dependency property would be nice.</p> <p>Anyways, here's some code:</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Controls; using System.Windows; using System.Collections; namespace WpfApplication1 { public class CollectionHelper : Control { public static DependencyProperty CollectionProperty = DependencyProperty.Register( "Collection", typeof(IEnumerable), typeof(CollectionHelper), new FrameworkPropertyMetadata(OnCollectionChanged)); public IEnumerable Collection { get { return GetValue(CollectionProperty) as IEnumerable; } set { SetValue(CollectionProperty, value); } } private static void OnCollectionChanged(object rawSender, DependencyPropertyChangedEventArgs args) { CollectionHelper sender = (CollectionHelper)rawSender; IEnumerable value = args.NewValue as IEnumerable; if(value==null) { sender.First = null; sender.Last = null; return; } bool isFirstSet = false; object lastItemTemp = null; foreach (var item in value) { if (!isFirstSet) { sender.First = item; isFirstSet = true; } lastItemTemp = item; } if (!isFirstSet) sender.First = null; sender.Last = lastItemTemp; } public DependencyProperty FirstProperty = DependencyProperty.Register( "First", typeof(object), typeof(CollectionHelper)); public object First { get { return GetValue(FirstProperty); } set { SetValue(FirstProperty, value); } } public DependencyProperty LastProperty = DependencyProperty.Register( "Last", typeof(object), typeof(CollectionHelper)); public object Last { get { return GetValue(LastProperty); } set { SetValue(LastProperty, value); } } } } </code></pre> <p>And an actual use case:</p> <pre><code>&lt;Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:Helper="clr-namespace:WpfApplication1" Title="MainWindow" Height="350" Width="525"&gt; &lt;StackPanel&gt; &lt;Helper:CollectionHelper x:Name="Helper" Collection="{Binding SomeStrings}" /&gt; &lt;TextBlock Text="{Binding ElementName=Helper, Path=First}" /&gt; &lt;TextBlock Text="{Binding ElementName=Helper, Path=Last}" /&gt; &lt;/StackPanel&gt; &lt;/Window&gt; </code></pre> <p>So the idea here would be to grab the first and last item using the relay control and then bind to there margins.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload