简体   繁体   English

XAML背后的Silverlight代码

[英]Silverlight codebehind from XAML

I have the following code in XAML: 我在XAML中有以下代码:

<data:DataGridTemplateColumn>

   <data:DataGridTemplateColumn.CellTemplate>

      <DataTemplate>

         <Image x:Name="picture" Width="200" Height="130" Visibility="Visible"/> 

      </DataTemplate>

   </data:DataGridTemplateColumn.CellTemplate>

</data:DataGridTemplateColumn>

How would do I this in the code behind (C#), without using this XAML code? 在不使用此XAML代码的情况下,如何在(C#)后面的代码中这样做?

EDIT: 编辑:

Here is the solution I am using: 这是我正在使用的解决方案:

Creating a Silverlight DataTemplate in code 用代码创建Silverlight DataTemplate

This lets me do exactly what I want. 这使我可以按照自己的意愿去做。

For example, you have a simple DataGrid 例如,您有一个简单的DataGrid

    <sdk:DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Items}">
        <sdk:DataGrid.Columns>
            <sdk:DataGridTemplateColumn CellTemplate="{StaticResource ImageCellTemplate}"/>
        </sdk:DataGrid.Columns>
    </sdk:DataGrid>

If you want to have several templates applied to a single DataGridRow, you can change visibility of internal parts inside the template: 如果要将多个模板应用于一个DataGridRow,则可以更改模板内部零件的可见性:

    <DataTemplate x:Key="ImageCellTemplate">
        <Grid>
            <StackPanel Orientation="Horizontal" Visibility="{Binding IsImageTemplate}">
                <Image Width="20" Height="20"/>
                <TextBlock Text="{Binding Title}"/>
            </StackPanel>
            <StackPanel Orientation="Horizontal" Visibility="{Binding IsTextBoxTemplate}">
                <TextBlock Text="{Binding Id}" Foreground="Red"/>
                <TextBox Text="{Binding Title}"/>
            </StackPanel>
        </Grid>
    </DataTemplate>

There is the part with Image and the part with TextBox that are bound to the properties of an item view model (IsImageTemplate and IsTextBoxTemplate respectively). 其中有Image的部分和TextBox的部分绑定到项目视图模型的属性(分别为IsImageTemplate和IsTextBoxTemplate)。 They are mutually exclusive and panels will not cover each other. 它们是互斥的,面板不会互相覆盖。

public partial class MainPage : UserControl
{
    public MainPage()
    {
        InitializeComponent();
        var items = new List<ItemViewModel>()
        {
            new ItemViewModel{Id = 1, Title="First", IsImage = true},
            new ItemViewModel{Id = 2, Title="Second", IsImage = false},
            new ItemViewModel{Id = 3, Title="Third", IsImage = false}
        };
        this.DataContext = new MainViewModel { Items = items };
    }
}

public class MainViewModel
{
    public List<ItemViewModel> Items { get; set; }
}

public class ItemViewModel
{
    public int Id { get; set; }
    public string Title { get; set; }
    public bool IsImage { get; set; }

    public Visibility IsImageTemplate
    {
        get { return (IsImage == true) ? Visibility.Visible : Visibility.Collapsed; }
    }

    public Visibility IsTextBoxTemplate
    {
        get { return IsImage == false ? Visibility.Visible : Visibility.Collapsed; }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM