我需要在列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
我做错了什么?
发布于 2018-08-20 23:58:56
用DockPanel/StackPanel替换WrapPanel,它应该可以正常工作。事实上,你不需要事件面板,代码本身就足够了:
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));
}
}https://stackoverflow.com/questions/51933914
复制相似问题