【问题标题】:WPF Grid Column width in DataTemplate does not workDataTemplate 中的 WPF 网格列宽不起作用
【发布时间】:2016-02-23 17:50:42
【问题描述】:

我有DataWindowUserControls(不同的ViewModels)。

我的DataWindow.Xaml

<catel:DataWindow.Resources>
<DataTemplate DataType="{x:Type viewmodels:MessageViewModel}">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="20*"/>
            <ColumnDefinition Width="80*"/>
        </Grid.ColumnDefinitions>
        <Views:MessageView Grid.Column="1"/>
    </Grid>
</DataTemplate>
</catel:DataWindow.Resources>

<ItemsControl ItemsSource="{Binding Messages}">

我的UserControlMessageView.Xaml

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
    </Grid.RowDefinitions>

    <Border Grid.Row="0" Background="Red">
    </Border>

    <Border Grid.Row="1">
        ...
        Content
        ...
    </Border>
</Grid>

Messages : ObservableCollection&lt;ViewModelBase&gt;();

我在UserControl 中的内容会在运行时动态添加到DataWindow。如果UserControl 中的内容宽度大于 WindowWidth ColumnWidth(column1 20* 和 column2 80*)不起作用。我只看到 Grid.Column(80*),它的宽度是 100*。我究竟做错了什么? 感谢您的帮助!

【问题讨论】:

  • 您确定您的Views:MessageView 知道父Grid - stackoverflow.com/questions/17914005/… 吗?
  • 感谢您的帮助!问题是“当元素的大小大于窗口大小时,不可能渲染 80% 的窗口”

标签: c# xaml mvvm catel


【解决方案1】:

当元素的大小大于窗口大小时,不可能渲染 80% 的窗口。 为了解决这个问题,您可以通过删除代码中的 Windows Width 属性来使窗口大小动态化。

【讨论】:

  • 谢谢!查看添加后我可以重新渲染窗口吗?如果我重新渲染窗口 - 它是 20 和 80?
  • 是的,当您使用 WPF 绑定时,如果您的 ViewModel 实现 INotifyPropertyChanged 并且您的列表必须是 ObservableCollection,则通过更改 ViewModel 重新呈现自身。
猜你喜欢
  • 2020-06-25
  • 1970-01-01
  • 2021-04-12
  • 1970-01-01
  • 2018-09-03
  • 1970-01-01
  • 2018-10-08
  • 2011-07-06
  • 1970-01-01
相关资源
最近更新 更多