I have defined this in XAML:

<phone:PhoneApplicationPage.Resources>
    <DataTemplate x:Key="sendContentTemplate">
        <StackPanel>
            <TextBlock Name="P1Label" Text="1" Visibility="Collapsed" />
            <TextBox Name="P1" Visibility="Collapsed" />
            <TextBlock Name="P2Label" Text="2" Visibility="Collapsed" />
            <TextBox Name="P2" Visibility="Collapsed" />
            <TextBlock Name="P3Label" Text="3" Visibility="Collapsed" />
            <TextBox Name="P3" Visibility="Collapsed" />
        </StackPanel>
    </DataTemplate>
</phone:PhoneApplicationPage.Resources>

Then, in code, I define a CustomMessageBox from Windows Phone Toolkit in this way:

DataTemplate contentTemplate = (DataTemplate)Resources["sendContentTemplate"];
SetVisibility(Contrato[0], contentTemplate);
var messageBox = new CustomMessageBox 
        {
            Caption = "Send",
            Message = "",
            ContentTemplate = contentTemplate,
            LeftButtonContent = "Send",
            RightButtonContent = "Close"
        };         
messageBox.Show();

I'm trying to modify properties in the DataTemplate in code, but without success. Now, I'm doing this:

private void SetVisibility(Contrato contrato, DataTemplate dataTemplate)
{
    var controls = AllChildren(dataTemplate.LoadContent());
    if (contrato.Number == 1)
    {
        controls.Find(c => c.Name == "P1Label").Visibility = Visibility.Visible;
        controls.Find(c => c.Name == "P1").Visibility = Visibility.Visible;            
    }
}

private List<FrameworkElement> AllChildren(DependencyObject parent)
{
    var list = new List<FrameworkElement>();
    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
    {
        var child = VisualTreeHelper.GetChild(parent, i);
        if (child is FrameworkElement)
        {
            list.Add((FrameworkElement)child);
        }

        list.AddRange(AllChildren(child));
    }

    return list;
}

Is it possible to modify a DataTemplate property by code?

有帮助吗?

解决方案

Finally, I did it by adding controls by code. I couldn't find another way to do it. I did it in this way:

var messageBox = new CustomMessageBox {
        Caption = "Send",
        Message = "",
        Content =  CreateMessageBoxContent(c.T),
        LeftButtonContent = "Send",
        RightButtonContent = "Close"
    }; 


private static object CreateMessageBoxContent(string t)
    {
        var stackPanel = new StackPanel();
        var P1Label = new TextBlock {Text = AppResources.P1Label};
        var P2Label = new TextBlock {Text = AppResources.P2Label};
        var P3Label = new TextBlock {Text = AppResources.P3Label};

        var P1 = new TextBox();
        var P2 = new TextBox();
        var P3 = new TextBox();

            if (t == "T2" || t == "T20")
            {
                stackPanel.Children.Add(P1Label);
                stackPanel.Children.Add(P1);
            }
            else if (t =="T20D" || t == "T21D")
            {
                stackPanel.Children.Add(P1Label);
                stackPanel.Children.Add(P1);

                stackPanel.Children.Add(P3Label);
                stackPanel.Children.Add(P3);
            }
            else if (t == "T3" || t == "T31")
            {
                stackPanel.Children.Add(P1Label);
                stackPanel.Children.Add(P1);

                stackPanel.Children.Add(P2Label);
                stackPanel.Children.Add(P2);

                stackPanel.Children.Add(P3Label);
                stackPanel.Children.Add(P3);
            }
        return stackPanel;            
    }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top