wpf datagrid DataGridTemplateColumn.CellTemplate文本框Horizo​​ntalAlignment

时间:2018-08-20 15:21:39

标签: wpf datagrid datatemplate datagridtemplatecolumn

我需要在水平的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

我在做什么错了?

1 个答案:

答案 0 :(得分:1)

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

ItemsSource dataGrid

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));
    }
}