简体   繁体   中英

Custom control not being set to content width

I am trying to build a calendar segmented control but when I view the control at runtime its width is cut off.

I am declaring my controls like this so i think the reason is cause the xaml is not setting the width or something?

public class DateTimePicker2 : ContentView, INotifyPropertyChanged
{

     public Entry _entry { get; private set; } = new Entry();

    public Entry _dayEntry { get; private set; } = new Entry();

    public Entry _monthEntry { get; private set; } = new Entry();

    public Entry _yearEntry { get; private set; } = new Entry();

    public Entry _hourEntry { get; private set; } = new Entry();

    public Entry _minsEntry { get; private set; } = new Entry();


    private static DateTime? _defaultDateTime = DateTime.Now;


    public DatePicker _datePicker { get; private set; } = new DatePicker() { MinimumDate = DateTime.Today, IsVisible = false };
    public TimePicker _timePicker { get; private set; } = new TimePicker() { IsVisible = false };

    public DatePicker _datePicker { get; private set; } = new DatePicker() { 
    MinimumDate = DateTime.Today, IsVisible = false };
    public TimePicker _timePicker { get; private set; } = new TimePicker() 
    { IsVisible = false };

    public DateTimePicker2()
    {
        BindingContext = this;

        Content = new StackLayout()
        {
            Orientation = StackOrientation.Horizontal,
            Children =
        {
            _datePicker,
            _timePicker,
            _entry
        }
        };

        _datePicker.SetBinding<DateTimePicker2>(DatePicker.DateProperty, p => p._date);
        _timePicker.SetBinding<DateTimePicker2>(TimePicker.TimeProperty, p => p._time);
        
        
        _timePicker.Unfocused += (sender, args) => _time = _timePicker.Time;
        _datePicker.Focused += (s, a) => UpdateEntryText();

        GestureRecognizers.Add(new TapGestureRecognizer()
        {
            Command = new Command(() => _datePicker.Focus())
        });
        _entry.Focused += (sender, args) =>
        {
            Device.BeginInvokeOnMainThread(() => _datePicker.Focus());
        };
        _datePicker.Unfocused += (sender, args) =>
        {
            Device.BeginInvokeOnMainThread(() =>
            {
                _timePicker.Focus();
                _date = _datePicker.Date;
                UpdateEntryText();
            });
        };
    }

 private void UpdateEntryText()
 {
        _entry.Placeholder=_placeholderText;
        
        _entry.Text = DateTime.ToString(StringFormat);
 }

 static void DTPropertyChanged(BindableObject bindable, object oldValue, object newValue)
 {
        var timePicker = (bindable as DateTimePicker2);
        timePicker.UpdateEntryText();
 }
 }
} 

For some reason as you can see here in the image my entry text is only showing some of the content

This should be at least showing the content width what is going on here?

在此处输入图片说明

I consume it as follows

<local:DateTimePicker2 x:Name="txtDateStartEntry"
           PlaceholderText="Please enter Session date / time"
           isMonthPickerVisible="True"
           isDayPickerVisible="True"
           WidthRequest="300">

I found my reason i need to create my entrys like this.

public Entry _entry { get; private set; } = new Entry() { 
WidthRequest=300};

You will notice I added the width request but shouldnt that pick it up from the existing xaml if not how do I achieve that?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM