绑定不会更改Button.Content属性

时间:2017-04-23 10:07:03

标签: c# xaml inotifypropertychanged

模板中有Button,它的值绑定到INotifyPropertyChanged,但Content在运行时不会更改。它只更新一次。但如果我从后面的代码触发更改,则不会更改它。

据我所知,我已经完全正确地设置了代码,但我可能在某处出错了。我从点击按钮显示的Flyout触发更改,但不会更改按钮内容。

我的XAML代码是:

<DataTemplate x:Key="UserUploads">
        <UserControl>
            <RelativePanel Name="panel"
                           Margin="5">
                <Image Name="image" 
                       Source="{Binding VideoThumbnail, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"/>
                <TextBlock Text="{Binding VideoTitle, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
                           Name="title"
                           Style="{StaticResource SubtitleTextBlockStyle}"
                           TextTrimming="CharacterEllipsis"
                           MaxLines="2"/>
                <TextBlock Name="publish"
                           Text="{Binding PublishingTime, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"/>
                <TextBlock Name="privacy"
                           Text="{Binding PrivacyStatus, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"/>
                <Button Name="LikeBtn"
                        Background="Transparent"
                        RelativePanel.AlignBottomWithPanel="True"
                        Click="LikeBtn_Click"
                        Content="{Binding FavoriteBtnContent, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}">
                </Button>
                <VisualStateManager.VisualStateGroups>
                    <VisualStateGroup>
                        <VisualState>
                            <VisualState.StateTriggers>
                                <AdaptiveTrigger MinWindowWidth="0"/>
                            </VisualState.StateTriggers>
                            <VisualState.Setters>
                                <Setter Target="image.Width"
                                        Value="144"/>
                                <Setter Target="image.Height"
                                        Value="81"/>
                                <Setter Target="title.MaxLines"
                                        Value="1"/>
                                <Setter Target="title.(RelativePanel.Below)"
                                        Value="image"/>
                                <Setter Target="publish.(RelativePanel.Below)"
                                        Value="title"/>
                                <Setter Target="privacy.(RelativePanel.Below)"
                                        Value="publish"/>
                                <Setter Target="LikeBtn.(RelativePanel.Below)"
                                        Value="privacy"/>
                            </VisualState.Setters>
                        </VisualState>
                        <VisualState>
                            <VisualState.StateTriggers>
                                <AdaptiveTrigger MinWindowWidth="550"/>
                            </VisualState.StateTriggers>
                            <VisualState.Setters>
                                <Setter Target="image.Width"
                                        Value="256"/>
                                <Setter Target="image.Height"
                                        Value="144"/>
                                <Setter Target="title.MaxLines"
                                        Value="2"/>
                                <Setter Target="title.(RelativePanel.RightOf)"
                                        Value="image"/>
                                <Setter Target="publish.(RelativePanel.RightOf)"
                                        Value="image"/>
                                <Setter Target="privacy.(RelativePanel.RightOf)"
                                        Value="image"/>
                                <Setter Target="publish.(RelativePanel.Below)"
                                        Value="title"/>
                                <Setter Target="privacy.(RelativePanel.Below)"
                                        Value="publish"/>
                                <Setter Target="LikeBtn.(RelativePanel.RightOf)"
                                        Value="image"/>
                            </VisualState.Setters>
                        </VisualState>
                    </VisualStateGroup>
                </VisualStateManager.VisualStateGroups>
            </RelativePanel>
        </UserControl>
    </DataTemplate>

我的xaml.cs代码隐藏是:

public sealed partial class UserUploads : Page
{


    public UserUploads()
    {
        this.InitializeComponent();
        DataContext = Helpers.UserUploadedVideos.inotifyObj;
    }

    protected async override void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);
        // load data

        // load uploaded videos without live streams
        Helpers.UserUploadedVideos.inotifyObj.VideoUploadsList = await Helpers.APIData.GetUserUploads(Helpers.APIData.VideoType.Uploaded);
        // load uploaded live streams
        Helpers.UserUploadedVideos.inotifyObj.LiveStreamsList = await Helpers.APIData.GetUserUploads(Helpers.APIData.VideoType.Live);
        // load playlists

        // done
    }



    private void LikeBtn_Click(object sender, RoutedEventArgs e)
    {
        var item = (Helpers.UserUploadedVideos)((FrameworkElement)e.OriginalSource).DataContext;
        if (item.LikedOrNot == Helpers.UserUploadedVideos.VideoLike.none)
        {
            var menu1 = new MenuFlyoutItem() { Text = "Like" };
            var menu2 = new MenuFlyoutItem() { Text = "Dislike" };
            menu1.Click += (s, es) => 
            {
                LikeBtnFlyout_Click(s);
            };
            var panel = new StackPanel();
            panel.Children.Add(menu1);
            panel.Children.Add(menu2);
            var flyout = new Flyout() { Content = panel };
            flyout.ShowAt(sender as FrameworkElement);
        }
        else
        {
            var menu1 = new MenuFlyoutItem() { Text = "Remove Like/Dislike" };
            menu1.Click += (s, es) => { LikeBtnFlyout_Click(s); };
            var panel = new StackPanel();
            panel.Children.Add(menu1);
            var flyout = new Flyout() { Content = panel };
            flyout.ShowAt(sender as FrameworkElement);
        }
    }

    private void LikeBtnFlyout_Click(object sender)
    {
        var menuItem = sender as MenuFlyoutItem;
        var item = new Helpers.INotifyProperty();
        if (menuItem.Text == "Like")
        {
            // like the vide
            Helpers.UserUploadedVideos.inotifyObj.FavoriteBtnContent = new FontIcon() { Glyph = Helpers.SettingsKeys.LikeIcon };
        }
        else if (menuItem.Text == "Dislike")
        {
            // dislike the video
            Helpers.UserUploadedVideos.inotifyObj.FavoriteBtnContent = new FontIcon() { Glyph = Helpers.SettingsKeys.DislikeIcon };
        }
        else
        {
            // remove the like/dislike as in "none"
            Helpers.UserUploadedVideos.inotifyObj.FavoriteBtnContent = "Like/Dislike";
        }
    }
}

我的自定义课程INotifyPropertyChanged

public class UserUploadedVideos : INotifyPropertyChanged
{
    public enum VideoLike
    {
        like,
        dislike,
        none
    }

    public static UserUploadedVideos inotifyObj = new UserUploadedVideos();

    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged([CallerMemberName] string setting = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(setting));
    }

    private ObservableCollection<UserUploadedVideos> uploadedVideos;
    public ObservableCollection<UserUploadedVideos> VideoUploadsList
    {
        get { return uploadedVideos; }
        set
        {
            if (value != uploadedVideos)
            {
                uploadedVideos = value;
                OnPropertyChanged();
            }
        }
    }

    private ObservableCollection<UserUploadedVideos> liveSreams;
    public ObservableCollection<UserUploadedVideos> LiveStreamsList
    {
        get { return liveSreams; }
        set
        {
            if (value != liveSreams)
            {
                liveSreams = value;
                OnPropertyChanged();
            }
        }
    }

    private object btnContent;
    public object FavoriteBtnContent
    {
        get
        {
            if (btnContent == null)
            {
                btnContent = "Like/Dislike";
            }
            return btnContent;
        }
        set
        {
            if (value != btnContent)
            {
                btnContent = value;
                OnPropertyChanged();
            }
        }
    }

    private BitmapImage vThumbnail;
    public BitmapImage VideoThumbnail
    {
        get { return vThumbnail; }
        set
        {
            if (value != vThumbnail)
            {
                vThumbnail = value;
                OnPropertyChanged();
            }
        }
    }

    private string vTitle;
    public string VideoTitle
    {
        get { return vTitle; }
        set
        {
            if (value != vTitle)
            {
                vTitle = value;
                OnPropertyChanged();
            }
        }
    }

    private string publishTime;
    public string PublishingTime
    {
        get { return publishTime; }
        set
        {
            if (value != publishTime)
            {
                publishTime = value;
                OnPropertyChanged();
            }
        }
    }

    private string privacyStatus;
    public string PrivacyStatus
    {
        get { return privacyStatus; }
        set
        {
            if (value != privacyStatus)
            {
                privacyStatus = value;
                OnPropertyChanged();
            }
        }
    }
    private string vID;
    public string VideoID
    {
        get { return vID; }
        set
        {
            if (value != vID)
            {
                vID = value;
                OnPropertyChanged();
            }
        }
    }

    private string cID;
    public string ChannelID
    {
        get { return cID; }
        set
        {
            if (value != cID)
            {
                cID = value;
                OnPropertyChanged();
            }
        }
    }

    private VideoLike likeOrNot;
    public VideoLike LikedOrNot
    {
        get { return likeOrNot; }
        set
        {
            if (value != likeOrNot)
            {
                likeOrNot = value;
                OnPropertyChanged();
            }
        }
    }

    private string pageToken;
    public string PageToken
    {
        get { return pageToken; }
        set
        {
            if (value != pageToken)
            {
                pageToken = value;
                OnPropertyChanged();
            }
        }
    }

}

所有其他属性都能很好地工作,因为我不需要在第一次之后更改它们,但我确实需要随时更改按钮内容。

所以请帮我理清这里出了什么问题。感谢

更新

我稍微更改了代码并将x:Bind与整个列表一起使用,并尝试从其发件人对象更新按钮并且它可以正常工作。如果我从发件人对象中更改Button内容,我可以立即看到更改。

0 个答案:

没有答案