Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Sounds like a fun project! I've implemented databinding on nested content controls within a button before using declarations similar to the following. So in your case the push pin collection would be bound to the items control with each push pin object providing the data for its corresponding button (including the button's nested image and textblock).</p> <p>Let's take a look at a simple example that I hope will guide you in the right direction.</p> <p>To start here's an example of a button template that you could define in the resource dictionary of your choosing. Note the visibility binding on the image and the text binding on the textblock, these properties will be located on a Pushpin_ViewModel we'll define later:</p> <pre><code> &lt;Style x:Name="PushpinButtonStyle" TargetType="Button"&gt; &lt;Setter Property="Template"&gt; &lt;Setter.Value&gt; &lt;ControlTemplate TargetType="Button"&gt; &lt;Grid&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition Width="*" /&gt; &lt;ColumnDefinition Width="*" /&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;VisualStateManager.VisualStateGroups&gt; &lt;VisualStateGroup x:Name="FocusStates"&gt; &lt;VisualState x:Name="Focused"/&gt; &lt;VisualState x:Name="Unfocused"/&gt; &lt;/VisualStateGroup&gt; &lt;VisualStateGroup x:Name="CommonStates"&gt; &lt;VisualState x:Name="Normal" /&gt; &lt;VisualState x:Name="MouseOver" /&gt; &lt;VisualState x:Name="Pressed"/&gt; &lt;VisualState x:Name="Disabled"/&gt; &lt;/VisualStateGroup&gt; &lt;/VisualStateManager.VisualStateGroups&gt; &lt;Image Grid.Column="0" Grid.Row="0" Grid.RowSpan="2" Height="50" Width="50" Source="Pushpins/pushpinSeaplane.png" Visibility="{Binding PushpinImageVisibility}" /&gt; &lt;Grid Grid.Column="1"&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition Height="39" /&gt; &lt;RowDefinition Height="*" /&gt; &lt;/Grid.RowDefinitions&gt; &lt;Grid Grid.Row="0" Background="Black"&gt; &lt;TextBlock Grid.Row="0" Foreground="White" Text="{Binding PushpinLabelText}" TextWrapping="Wrap" Margin="5" /&gt; &lt;/Grid&gt; &lt;/Grid&gt; &lt;/Grid&gt; &lt;/ControlTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; </code></pre> <p>In your primary view where your pushpins are displayed you may have some sort of items control responsible for displaying your pushpins. Here is an example of such an items control where the data template is a button with two important features 1) the button style we defined above and 2) a click event that will call a toggle method on the corresponding pushpin view model:</p> <pre><code>&lt;ItemsControl ItemsSource="{Binding Pushpins}"&gt; &lt;ItemsControl.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;Button Name="PushpinButton" Click="pushpinButton_Click" DataContext="{Binding}" Style="{StaticResource PushpinButtonStyle}" /&gt; &lt;/DataTemplate&gt; &lt;/ItemsControl.ItemTemplate&gt; &lt;/ItemsControl&gt; private void pushpinButton_Click(object sender, RoutedEventArgs e) { Pushpin_ViewModel pushpin_ViewModel = ((Button)sender).DataContext as Pushpin_ViewModel; pushpin_ViewModel.TogglePushpinVisibility(); } </code></pre> <p>The following view model class would represent the data context of your primary view (the one that contains the items control we defined above). Here we have the collection of pushpins that populate the items control:</p> <pre><code>public class PrimaryPushpinView_ViewModel : INotifyPropertyChanged { public PushpinView_ViewModel() { this.Pushpins.Add(new Pushpin_ViewModel() { PushpinLabelText="First Pushpin" }); } public List&lt;Pushpin_ViewModel&gt; Pushpins { get { return pushpins; } set { if (value != pushpins) { pushpins = value; OnPropertyChanged("Pushpins"); } } } private List&lt;Pushpin_ViewModel&gt; pushpins = new List&lt;Pushpin_ViewModel&gt;(); } </code></pre> <p>And finally here is a representation of the pushpin view model. There would be one instance of this class per pushpin in your pushpin collection:</p> <pre><code>public class Pushpin_ViewModel : INotifyPropertyChanged { public Visibility PushpinVisibility { get { return pushpinVisibility; } set { if (value != pushpinVisibility) { pushpinVisibility= value; OnPropertyChanged("PushpinVisibility"); } } } private Visibility pushpinVisibility; public String PushpinLabelText { get { return pushpinLabelText; } set { if (value != pushpinLabelText) { pushpinLabelText= value; OnPropertyChanged("PushpinLabelText"); } } } private String pushpinLabelText; public void TogglePushpinVisibility() { this.PushpinVisibility = this.PushpinVisibility.Equals(Visibility.Visible) ? Visibility.Collapsed : Visibility.Visible; } } </code></pre> <p>Sorry it took a while to get back to you, crazy day today, hope this helps out.</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.
 

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