开发者

Dynamically Load Content and Set Binding

开发者 https://www.devze.com 2023-04-01 02:56 出处:网络
I have a wizard control that will generate additional wizard pages based on devices selected from one of the screen. Once additional devices are set, the wizard will add create new wizard pages for th

I have a wizard control that will generate additional wizard pages based on devices selected from one of the screen. Once additional devices are set, the wizard will add create new wizard pages for those devices.

I have a user control 'SDContentTemplate' that is instantiated and the content of a content control is set by an object passed into the constructor. That object is XAML that is loaded from an XML datasource (CDATA).

<Section Order="1" RepeatPerDevice="true" Title="Report Data">
  <![CDATA[<DockPanel xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' xmlns:tk="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit">
    <Border DockPanel.Dock="Top" BorderBrush="Black" BorderThickness="1" Width="170" HorizontalAlignment="Left">
        <StackPanel>
            <TextBlock Margin="0 0 0 16" TextAlignment="Center">Device X of Y</TextBlock>
            <WrapPanel>
                <TextBlock MinWidth="80" Padding="4 2">Manufacturer:</TextBlock>
                <TextBlock MinWidth="80" Padding="4 2" Text="{Binding Path=SDDevice.Manufacturer}"></TextBlock>
            </WrapPanel>
            <WrapPanel>
                <TextBlock MinWidth="80" Padding="4 2 4 6">Serial Number:</TextBlock>
                <TextBlock MinWidth="80" Padding="4 2 4 6" Text="{Binding Path=SerialNumber}"></TextBlock>
            </WrapPanel>
        </StackPanel>
    </Border>
</DockPanel>]]>
</Section>

The SDContentTemplate control has a public object Device that is set by the code creating this new user control, and I am setting that object when it is instantiated.

The content xaml is all loaded, and everything shows up correctly except the data binding is not working. No data is being displayed within the textblocks. I have tried setting the data context of the 'ContentControl' as well as setting the data context of the 'SDContentTemplate' but I have not been able to figure out how to get the datacontext correctly set so that the textblocks are populated with the data values.

Any suggestions on what I am doing wrong? Am I mis开发者_Go百科taken and this is not possible?

Thanks!


I would suggest downloading something like Snoop to see what the actual DataContext is at runtime

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号