2011-12-23 2 views
0

간단한 문제를 해결하여 머리를 부러 뜨 렸습니다. 템플릿 속성이 채워진 사용자 지정 컨트롤이 있습니다. 템플릿은 내부에 TextBox가있는 간단한 Grid입니다. 이 텍스트 상자는 setter 및 getter를 사용하여 싱글 톤의 proprty에 바인딩됩니다. 프로그래밍 방식으로 Singleton에서 값을 읽고 다시 TextBox를 강제로 적용 할 수 있습니까?button2_UpdateControl 및 button1_UpdateSource 방법으로 구현되어야 하는지를 즉TemplateControl에서 데이터 소스를 업데이트하는 방법

<Window x:Class="Spike.Window1" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="Window1" Height="305" Width="521" xmlns:my="clr-namespace:Spike" xmlns:Data="clr-namespace:Spike.Data"> 
    <Grid> 
     <Grid.Resources> 
      <ControlTemplate x:Key="editingTemplate"> 
       <Grid> 
        <TextBox Text="{Binding Source={x:Static Data:MyClass.Instance}, Path=Value2}"/> 
       </Grid> 
      </ControlTemplate> 
     </Grid.Resources> 

     <UserControl Template="{StaticResource editingTemplate}" HorizontalAlignment="Left" Margin="58,60,0,0" x:Name="myUserControl1" VerticalAlignment="Top" Height="75" Width="284" /> 
     <Button Content="Update source" Height="23" HorizontalAlignment="Left" Margin="184,23,0,0" Name="button1" VerticalAlignment="Top" Width="111" Click="button1_UpdateSource" Focusable="False" /> 
     <Button Content="Update control" Focusable="False" Height="23" HorizontalAlignment="Left" Margin="58,23,0,0" Name="button2" VerticalAlignment="Top" Width="111" Click="button2_UpdateControl" /> 
    </Grid> 
</Window> 
namespace Spike.Data 
{ 
    public class MyClass 
    { 
     private static readonly MyClass MyClassInstance = new MyClass(); 

     public MyClass() 
     { 
      Value1 = "value1"; 
      Value2 = "value2"; 
     } 

     public static MyClass Instance 
     { 
      get { return MyClassInstance; } 
     } 

     public string Value1 { get; set; } 

     public string Value2 { get; set; } 
    } 
} 

?

수동으로 다시 호출 할 수 없습니다해야 바인딩 도움

답변

0

A의 사전에 감사합니다. 대신, 현재 바인딩 메커니즘을 올바르게 작동시켜야합니다. TextBox가 DependancyProperty에 바인딩되거나 TextBox의 DataContext가 INotifyPropertyChanged을 구현해야합니다. 이처럼 Value2 속성을 정의합니다

public static readonly DependencyProperty Value2Property = 
    DependencyProperty.Register("Value2", typeof(string), typeof(MyClass), new PropertyMetadata(string.Empty)); 

public string Value2 
{ 
    get { return (string)GetValue(Value2Property); } 
    set { SetValue(Value2Property, value); } 
} 

또한, MyClass에이 UIElement에 구현해야합니다.

+0

MyClass는 INotifyPropertyChanged를 구현할 수 있습니다. - 이것은 문제가되지 않습니다. 하지만 그 템플릿에 대해 아무런 지식도없이 UserControl이 바운드 개체의 TextBox 업데이트 값을 강제로 가져올 수있는 방법을 알고 싶습니다. – user1113996

+0

UserControl에서 TextBox를 업데이트하지 않습니다. TextBox는 이벤트에 바인딩하고 해당 이벤트에 응답하므로 TextBox를 업데이트합니다. UserControl은 TextBox가 바인딩되는 대상을 알지 못합니다. 별도의 바인딩 컨텍스트를 가질 수있는 다른 컨트롤에 대한 컨테이너 역할을합니다. –

+0

자, 사용자 정의 컨트롤이 여러 템플릿을 가질 수있는 격자의 셀이라고 가정 해 봅니다. 2. 데이터 편집 중 다른 템플리트. 사용자가 셀을 클릭하면 격자가 템플릿을 변경합니다. 새 템플릿을 설정하는 동안 데이터 객체에서 값을 가져옵니다. 편집 컨트롤이 포커스를 잃었을 때 - 일부 유효성 검사 프로세스로 propertie의 setter를 호출하려고 시도합니다. 이 컨트롤이 포커스를 잃지 않고 프로그래밍 방식으로 수정 된 값을 커밋하도록 강제 할 수 있습니까? – user1113996

관련 문제