2012-02-29 3 views
0

DependencyProperty를 사용하여 바인딩하려하지만 DependencyProperty를 바인드하지 않고 작업 할 수 없습니다.Silverlight에서 DependencyProperty를 작동 시키려면 어떻게해야합니까?

저는 실버 라이트 가이드를 따라 왔으며 지금까지 XAML을 사용하여 속성을 설정할 수있게되었습니다.

에서 MainPage.xaml :

<UserControl 
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" 
xmlns:local="clr-namespace:UserControlSample" x:Class="UserControlSample.MainPage" 
mc:Ignorable="d" 
d:DesignHeight="300" d:DesignWidth="400"> 

<Grid x:Name="LayoutRoot" Background="White"> 
    <local:InfoRectangle Margin="32,36,0,0" HorizontalAlignment="Left" Height="70" VerticalAlignment="Top" Width="122" InfoText="New Text"/> 
    <local:InfoRectangle Margin="105,139,188,97" InfoText="some text" /> 
</Grid> 

InfoRectangle.xaml :

<UserControl 
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" 
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" 
mc:Ignorable="d" 
x:Class="UserControlSample.InfoRectangle" 
d:DesignWidth="122" d:DesignHeight="70"> 

<Grid x:Name="LayoutRoot"> 
    <Rectangle Fill="#FFABABE9" Stroke="Black" RadiusY="4" RadiusX="4"/> 
    <TextBlock Name="InfoLabel" Text="Text block" Margin="5" /> 
</Grid> 

InfoRectangle.xaml.cs : 다음 코드는 내가 지금까지 가지고있다

using System; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Documents; 
using System.Windows.Ink; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Animation; 
using System.Windows.Shapes; 

namespace UserControlSample 
{ 
public partial class InfoRectangle : UserControl 
{ 
    public InfoRectangle() 
    { 
     // Required to initialize variables 
     InitializeComponent(); 
    } 

    public string InfoText 
    { 
     get { return (string)GetValue(InfoTextProperty); } 
     set { SetValue(InfoTextProperty, value); } 
    } 

    public static readonly DependencyProperty InfoTextProperty = 
     DependencyProperty.Register(
      "InfoText", 
      typeof(string), 
      typeof(InfoRectangle), 
      new PropertyMetadata("something", InfoTextChanged)); 

    private static void InfoTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
    { 
    } 
} 
} 

솔루션을 실행하면 두 개의 사각형이 나타나지만 기본 컨트롤이나 사용자 컨트롤의 MainPage XAML에 설정된 값이 아닌 "텍스트 블록"만 표시됩니다.

답변

0

여기 해결책이 있습니다.

콜백 메소드를 완료해야합니다.

private static void InfoTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
{ 
    ((InfoRectangle)d).InfoLabel.Text = e.NewValue.ToString(); 
} 
+0

콜백이 비어 있음을 알았지 만 그 문제가 심지어 그 것으로 들어 가지 않았다고 가정했습니다. – ChrisF

1

내 대답은 here입니다. 뷰 모델의 특정 속성을 업데이트하는 종속성 속성에 대한 좋은 예입니다. 뷰 모델의 속성은 텍스트 블록에 바인딩되므로 알림이 변경되면 텍스트 블록이 업데이트됩니다.

관련 문제