【问题标题】:wpf datagrid DataGridTemplateColumn.CellTemplate TextBox HorizontalAlignmentwpf datagrid DataGridTemplateColumn.CellTemplate TextBox Horizo​​ntalAlignment
【发布时间】:2019-01-26 18:29:29
【问题描述】:

我需要在水平方向的 DataGrid 列中拉伸 TextBox。我试着这样做:

<DataGridTemplateColumn Header="Time from"  Width="3*">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
                <TextBox x:Name="txtTextBlock" Text="{Binding Path=TimeOfActions.StartTime, UpdateSourceTrigger=PropertyChanged}" IsReadOnly="{Binding Path=TimeOfActions.IsReadOnly}" HorizontalAlignment="Stretch"/>
        </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

但结果我得到: enter image description here

我做错了什么?

【问题讨论】:

  • 你试过设置Horizo​​ntalContentAlignment=Center吗?

标签: wpf datagrid datatemplate datagridtemplatecolumn


【解决方案1】:

将WrapPanel 替换为DockPanel/StackPanel,它应该可以按预期工作。事实上,你不需要一个面板跟随代码本身就足够了:

ItemsSource 数据网格

dgDirectoryConditions.ItemsSource = ((ImageItemsViewModel)DataContext).Directories.ConditionsDirectory;

ImageItemsViewModel.cs

internal class ImageItemsViewModel : DependencyObject
{
     ...
     public ObservableCollection<ConditionsDirectory> ConditionsDirectories { get; set; }
     ...
}

ConditionsDirectory.cs

public class ConditionsDirectory : BaseDirectory, INotifyPropertyChanged
{
    public ConditionsDirectory()
    {
        DurationOfParking = new DurationOfParking
        {
            IsReadOnly = true
        };
        TimeOfActions = new TimeOfActions
        {
            IsReadOnly = true
        };;
    }

    public TimeOfActions TimeOfActions { get; set; }

    public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged([CallerMemberName]string prop = "")
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(prop));
    }
}

TimeOfActions.cs

public class TimeOfActions : INotifyPropertyChanged
{
    [JsonProperty("start_time")]
    public string StartTime
    {
        get { return StartTimePrivate; }
        set
        {
            if (!string.IsNullOrWhiteSpace(value) || !string.IsNullOrWhiteSpace(EndTime))
            {
                IsReadOnly = false;
            }
            else
            {
                IsReadOnly = true;
            }

            StartTimePrivate = value;
        }
    }

    [JsonIgnore]
    private string StartTimePrivate { get; set; }

    [JsonProperty("end_time")]
    public string EndTime
    {
        get { return EndTimePrivate; }
        set
        {
            if (!string.IsNullOrWhiteSpace(value) || !string.IsNullOrWhiteSpace(StartTime))
            {
                IsReadOnly = false;
            }
            else
            {
                IsReadOnly = true;
            }

            EndTimePrivate = value;
        }
    }

    [JsonIgnore]
    private string EndTimePrivate { get; set; }

    [JsonIgnore]
    public bool IsReadOnly { get; set; }

    public override string ToString()
    {
        return $"{StartTime} - {EndTime}";
    }

    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged([CallerMemberName]string prop = "")
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(prop));
    }
}

【讨论】:

  • 我尝试不使用 WrapPanel,但没有成功
  • @AntonAnpilogov 你能从你的角度发布更多代码吗?我发布的代码对我来说工作正常。
  • @AntonAnpilogov 我的意思是 xaml 代码,而不是 ViewModel 代码!
  • @AntonAnpilogov 不知道发生了什么,但相同的代码在 .Net 4.5.2 上对我有用,所以除非你在 github 上发布你的代码有可重现的问题,否则我的双手被束缚了。
猜你喜欢
  • 2012-10-13
  • 2011-08-09
  • 1970-01-01
  • 2021-09-16
  • 2010-10-19
  • 1970-01-01
  • 1970-01-01
  • 2011-05-21
  • 2013-06-04
相关资源
最近更新 更多