2013-03-28 1 views
0

사용자가 제어하고자하는 UI 영역이 있습니다. 기본적으로 그들은 특정 영역의 모습을 정의하는 xaml 조각을 제공합니다.도메인 객체가 제공하는 xaml에 컨트롤 바인딩하기

public class Project 
{ 
    public string DisplaySpecificationXml { get; set; } 
} 

우리가 런타임에 볼 수 있도록 XAML을 알고 어떻게 도메인 개체의 속성을 결합 할 수있는 쉬운 방법이 있나요;

PS -보기 프로젝트는 런타임에 변경되므로 UI의 해당 영역을 업데이트해야합니다.

+2

: 영리의 비트와 함께 , 하나는 공예가 될 수있다. 일부 절대적으로 끔찍한 결과와 xaml ... – JerKimball

+0

이것은 외부 소비, 비즈니스 애플 리케이션을위한 애플 리케이션이 아닙니다. 사용자 정의 영역을 정의 할 사용자는 실제로 비즈니스를위한 개발자입니다. 여기에 엄청난 보안 위험은 없지만, 중요하다는 메모와 upvote의 가치에 감사드립니다. – Matt

답변

1
<Window x:Class="MiscSamples.RuntimeXAML" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:local="clr-namespace:MiscSamples" 
     Title="RuntimeXAML" Height="300" Width="300"> 
    <Window.Resources> 
     <local:stringToUIConverter x:Key="Converter"/> 
    </Window.Resources> 
    <UniformGrid Rows="1" Columns="2"> 
     <ListBox ItemsSource="{Binding Projects}" x:Name="Lst"/> 
     <ContentPresenter Content="{Binding SelectedItem.DisplaySpecificationXml, ElementName=Lst, Converter={StaticResource Converter}}"/> 
    </UniformGrid> 
</Window> 

코드 뒤에 :

public partial class RuntimeXAML : Window 
    { 
     public List<Project> Projects { get; set; } 

     public RuntimeXAML() 
     { 
      InitializeComponent(); 
      Projects = new List<Project> 
          { 
           new Project() 
            { 
             DisplaySpecificationXml = 
             "<StackPanel>" + 
             "<TextBlock FontWeight='Bold' Text='This is UserControl1'/>" + 
             "<ComboBox Text='ComboBox'/>" + 
             "</StackPanel>" 
            }, 
           new Project() 
            { 

            } 
          }; 
      DataContext = this; 
     } 
    } 

변환기 :

public class stringToUIConverter: IValueConverter 
    { 
     public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
     { 
      if (value == null || (!(value is string))) 
       return null; 

      var header = "<Grid xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' " + 
         "xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>"; 

      var footer = "</Grid>"; 

      var xaml = header + (string) value + footer; 

      var UI = System.Windows.Markup.XamlReader.Parse(xaml) as UIElement; 
      return UI; 
     } 

     public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
     { 
      throw new NotImplementedException(); 
     } 
    } 

결과 :

enter image description here

나는 당신이 암시 적으로 사용자를 신뢰 희망
+0

감사합니다. 첫 번째 패스에서는 잘 어울립니다. 나는 그것을 오늘 밤 두 번 확인하고, 내가 그것을 보자 마자 받아 들인다. – Matt

+0

+1 영리한 변환기 사용! – JerKimball

관련 문제