问题
I have a derived class from MediaTransportControls
and trying to disable previous and next track buttons based on some events. I wrote following
public class MyMediaTransportControls : MediaTransportControls
{
public static readonly DependencyProperty IsPreviousTrackButtonEnabledProperty = DependencyProperty.Register(
"IsPreviousTrackButtonEnabled", typeof(bool), typeof(MyMediaTransportControls), new PropertyMetadata(false, IsPreviousTrackButtonEnabledChangedCallback));
private static async void IsPreviousTrackButtonEnabledChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var mediaTransportControls = d as MyMediaTransportControls;
if(mediaTransportControls?.GetTemplateChild("PreviousTrackButton") is Button previousTrackButton)
{
await mediaTransportControls.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
previousTrackButton.IsEnabled = (bool)e.NewValue;
});
}
}
public bool IsPreviousTrackButtonEnabled
{
get { return (bool) GetValue(IsPreviousTrackButtonEnabledProperty); }
set { SetValue(IsPreviousTrackButtonEnabledProperty, value); }
}
}
I have used data binding to bind IsPreviousTrackButtonEnabled
with the ViewModel logic. However, I find that even when previousTrackButton.IsEnabled
is set it has no impact on the UI. Sometime, though, when debuging, I can see the impact on UI.
Update
The MediaTransportControls
provide its own ControlTemplate
and I wish not to create my own template and do data binding with it.
回答1:
Write Notify Property Changed Event Handler
INotifyPropertyChanged
来源:https://stackoverflow.com/questions/58282831/unable-to-update-uwp-ui-from-code-behind-using-dispatcher