Skip to content

Instantly share code, notes, and snippets.

@tmyt
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tmyt/518e176bd5e93fbcf837 to your computer and use it in GitHub Desktop.
Save tmyt/518e176bd5e93fbcf837 to your computer and use it in GitHub Desktop.

#UserControlのぷろぱてぃ UserControlにプロパティ作って、UserControlからもそのプロパティを参照したいときに何作る?というはなし

##元のコード

<UserControl
    x:Class="App1.MyUserControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300"
    d:DesignWidth="400" x:Name="Root">

    <TextBlock />
</UserControl>
using System;
using System.Reactive.Linq;
using System.Threading;
using Windows.ApplicationModel;
using Windows.UI.Xaml.Controls;

namespace App1
{
    public sealed partial class MyUserControl : UserControl
    {
        public MyUserControl()
        {
            this.InitializeComponent();
        }
    }
}

外からはBindingしない

<TextBlock Text="{Binding Hoge,ElementName=Root}"/>
public string Hoge { get; set; }

外からもBindingする

<TextBlock Text="{Binding Hoge,ElementName=Root}"/>
public string Hoge
{
    get { return (string)GetValue(HogeProperty); }
    set { SetValue(HogeProperty, value); }
}
public static readonly DependencyProperty HogeProperty =
    DependencyProperty.Register("Hoge", typeof(string), typeof(MyUserControl), new PropertyMetadata(null));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment