首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >wpf datagrid DataGridTemplateColumn.CellTemplate TextBox HorizontalAlignment

wpf datagrid DataGridTemplateColumn.CellTemplate TextBox HorizontalAlignment
EN

Stack Overflow用户
提问于 2018-08-20 23:21:39
回答 1查看 178关注 0票数 1

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

代码语言:javascript
复制
<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

我做错了什么?

EN

回答 1

Stack Overflow用户

发布于 2018-08-20 23:58:56

DockPanel/StackPanel替换WrapPanel,它应该可以正常工作。事实上,你不需要事件面板,代码本身就足够了:

ItemsSource dataGrid

代码语言:javascript
复制
dgDirectoryConditions.ItemsSource = ((ImageItemsViewModel)DataContext).Directories.ConditionsDirectory;

ImageItemsViewModel.cs

代码语言:javascript
复制
internal class ImageItemsViewModel : DependencyObject
{
     ...
     public ObservableCollection<ConditionsDirectory> ConditionsDirectories { get; set; }
     ...
}

ConditionsDirectory.cs

代码语言:javascript
复制
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

代码语言:javascript
复制
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));
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51933914

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档