2013-11-21 2 views
0

두 개의 이미지를 사용자 지정 usercontrol에 아웃소싱하려고합니다. 두 개의 이미지를 설정하려면 두 개의 속성이 있어야하며 각 이미지의 원본에 대해 하나씩 설정해야합니다.사용자 지정 속성이있는 WPF Usercontrol

하지만 제대로 인식되지 않는 datacontext로 문제가 발생했습니다. 또한 문제가 될 수도 있습니다. 처음으로 종속성 속성을 사용한다는 것입니다. 어쨌든, 난 당신이 내 생각을 파악하고 여기에 날 도울 수 있기를 바랍니다, 여기에 소스 코드를 제공 :

MainViewModel : 내 MainWindow를보기에서

public class MainWindowViewModel : INotifyPropertyChanged 
{ 
    private string _spielerL1; 
    private string _spielerL2; 

    public MainWindowViewModel() 
    { 
     SpielerL1 = System.IO.Directory.GetCurrentDirectory() + @"\Images\queen_of_clubs.png"; 
     SpielerL2 = System.IO.Directory.GetCurrentDirectory() + @"\Images\queen_of_diamonds.png"; 
    [...] 
} 

    public string SpielerL1 
    { 
     get { return _spielerL1; } 
     private set 
     { 
      _spielerL1 = value; 
      OnPropertyChanged("SpielerL1"); 
     } 
    } 

    public string SpielerL2 
    { 
     get { return _spielerL2; } 
     private set 
     { 
      _spielerL2 = value; 
      OnPropertyChanged("SpielerL2"); 
     } 
    } 
} 

나는 단지의 ViewModel을 인스턴스화하고 SourceLeft와 컨트롤을 사용하고 있습니다 = "{바인딩 SpielerL1 ▣"SourceRight = "{SpielerL2 바인딩}"... 내 제어 코드 뒤에 다음과 같습니다

(삭제 sourceright은 짧게하기 위해) :

public partial class HandControl 
{ 
    public HandControl() 
    { 
     InitializeComponent(); 
     DataContext = this; 
    } 

    public string SourceLeft 
    { 
     get 
     { 
      return (string) GetValue(SourceLeftProperty); 
     } 
     set 
     { 
      SetValue(SourceLeftProperty, value); 
     } 
    } 
    public static readonly DependencyProperty SourceLeftProperty = DependencyProperty.Register("SourceLeft", typeof(string), typeof(HandControl), new PropertyMetadata("")); 
} 

그리고 마지막으로 데이터 컨텍스트 인식을 얻거나, 내 이미지가 표시되지 않는 밤은 내 UserControl을 XAML을, : 아직 WPF와의 UserControls와 많은 일을 havent 한 이후

<UserControl x:Class="FoolMe.Gui.Controls.HandControl" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300"> 
    <Grid> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="*" /> 
      <ColumnDefinition Width="3*" /> 
     </Grid.ColumnDefinitions> 
     <Image Grid.Column="1" 
       Source="{Binding SourceLeft}" /> 
     <Image Grid.Row="0" 
       Grid.Column="0" 
       Grid.ColumnSpan="2" 
       Source="{Binding SourceRight}" /> 
    </Grid> 
</UserControl> 

가, 내가 잘못 뭐죠 단서,이 없다. usercontrol 그것없이 잘 작동하지만,이 같은 아웃소싱, 내 창 "흰색"유지합니다.

누구나 아이디어가 있었는데, 무엇이 잘못 됐습니까?

+0

관련없는 : 대 = Path.Combine (http://msdn.microsoft.com/en-us/library/system.io.path.combine (V를 사용합니다. 110) .aspx)를 사용하여 경로 부분을 결합합니다. – Athari

+0

어쨌든 고마워, 항상 제안을 위해 열어주세요 :) – Jannik

답변

2

UserControlDataContext을 자체로 설정하면 안됩니다. 그러나 실제 문제는 에서 Image 요소에 발생합니다. 당신은 대신 RelativeSource Binding 사용해야합니다

<Image Grid.Column="1" Source="{Binding SourceLeft, RelativeSource={RelativeSource 
    AncestorType={x:Type YourXmlNamespacePrefix:HandControl}}}" /> 
<Image Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" Source="{Binding SourceRight, 
    RelativeSource={RelativeSource AncestorType={x:Type YourXmlNamespacePrefix: 
    HandControl}}}" />