如何通过代码更改datatemplate

时间:2010-10-21 14:27:04

标签: c# wpf xaml listbox datatemplate

我有ListBox和DataTemplate

我需要Set GroupBox Heigth = 300

怎么做?

<DataTemplate x:Key="data_template">
    <GroupBox Header="Категория" Width="300"  HorizontalAlignment="Stretch" x:Name="GroupBox">
    <DockPanel Tag="{Binding id}">
        <Button Click="Button_Click" DockPanel.Dock="Top" >
        <Button.Content>
            <DockPanel>
            <TextBlock Text="{Binding title}" TextWrapping="Wrap" DockPanel.Dock="Top" Padding="5"  HorizontalAlignment="Center"  Foreground="#FFB51414" />
            <l:ScrollViewerEx  VerticalScrollBarVisibility="Auto" >
                <TextBlock Text="{Binding description}" DockPanel.Dock="Top" TextWrapping="Wrap" Padding="5" IsHitTestVisible="False" />
            </l:ScrollViewerEx>
            </DockPanel>
        </Button.Content>
        </Button>
    </DockPanel>
    </GroupBox>
</DataTemplate>

2 个答案:

答案 0 :(得分:1)

如果有人试图解决我之前提出的问题,我会按以下方式解决:

DataTemplate mycolumnDataTemplate = null;
var dataTemplateStream = new SomeClass().GetType().Assembly.GetManifestResourceStream("Some.Namespace.SomeReosurceName.xaml");
string dataTemplateString = new System.IO.StreamReader(dataTemplateStream).ReadToEnd();
dataTemplateString = dataTemplateString.Replace("[0]", browserColumn.ColumnName);
mycolumnDataTemplate = XamlReader.Load(dataTemplateString) as DataTemplate;

答案 1 :(得分:0)

你想要达到什么目的?您是否希望在应用程序的运行时更改GroupBox高度,何时发生某些事件或某些数据已更改?如果是这样,那么您可能正在寻找的是数据触发器或事件触发器,您只需将其添加到DataTemplate中。