2014-11-17 2 views
1

변수의 내용을 렌더링하려는 문제가 있습니다 (아래 코드의 "Metatag.Configuration [VisualizerCode] .Value"식을 통해 참조).XAML - 변수 내용 렌더링

<Grid Margin="0,10,0,0" Visibility="Visible"> 
    <ContentControl Content="{Binding Path=Metatag.Configuration[VisualizerCode].Value}"> 
</ContentControl> 
: 나는 변수의 내용을 렌더링하고자하는 그리드를 가지고 내 응용 프로그램에서

<Grid> 
<Canvas> 
    <Border Canvas.Top="0" Canvas.Left="390" Width="50" Height="100" BorderThickness="2" BorderBrush="Black"> </Border> 
    <Border Canvas.Top="100" Canvas.Left="340" Width="100" Height="50" BorderThickness="2" BorderBrush="Black"> </Border> 
</Canvas> 
</Grid> 

: 변수는 다음과 같은 내용을 예를 들어, (문자열로) XAML 코드를 포함

불행히도, 이것을 실행하면 문자열 (= 해석되지 않은 변수의 내용)이 해석되는 대신 그리드에서 텍스트로 인쇄됩니다.이 경우 2 개의 멋진 테두리를 그려야합니다.

XAML에서 변수의 내용을 해석하여 렌더링 할 수있는 방법은 무엇입니까?

감사합니다.

Woelund

+0

[런타임에서 XAML로드 중입니까?] (http://stackoverflow.com/questions/910814/loading-xaml-at-runtime) –

답변

1
당신은 Grid의 일부 인스턴스에 문자열을 (구문 분석)을 변환하는 일부 사용자 지정 Converter를 사용하여 시도 할 수 있습니다

:

public class StringToElementConverter : IValueConverter { 
    public object Convert(object value, Type targetType, object parameter, 
             CultureInfo culture){ 
     var pc = new ParserContext(); 
     pc.XmlnsDictionary[""] = 
        "http://schemas.microsoft.com/winfx/2006/xaml/presentation"; 
     pc.XmlnsDictionary["x"] = "http://schemas.microsoft.com/winfx/2006/xaml"; 
     return XamlReader.Parse(System.Convert.ToString(value), pc); 
    } 
    public object ConvertBack(object value, Type targetType, object parameter, 
              CultureInfo culture){ 
     throw new NotImplementedException(); 
    } 
} 

일부 자원으로 컨버터를 선언하고 XAML에서 바인딩에 사용 코드 :

<Window.Resources> 
    <local:StringToElementConverter x:Key="elementConverter"/> 
</Window.Resources> 

<ContentControl Content="{Binding Metatag.Configuration[VisualizerCode].Value, 
          Converter={StaticResource elementConverter}}"/> 

로컬 이름을 나타내는 local 프리픽스를 선언하는 방법을 알고 싶습니다. 변환기 클래스가 선언 된 espace.

+1

놀라운 ... 나는 공식적으로 감동했습니다! 고마워요 - 매력과 같은 작품 !! – Woelund

+0

'ParserContext'를 생성하는 대신'XamlReader.Parse'를 직접 사용하고 기본 네임 스페이스를 포함하여 자신 만의 네임 스페이스를 정의 할 수 있습니다 .. –

+0

@lll'ParserContext'를 사용하지 않고'XamlReader.Parse'에서 경험했습니다. I 이''와 같이 문자열 *** 안에'xmlns' ***을 포함시켜야합니다. ". 간단한 데모를 직접 체험 해보십시오. –

관련 문제