Xaml.Win8:如何将嵌套类绑定到datatemplate?

时间:2014-10-09 13:22:54

标签: data-binding winrt-xaml

我有自定义元素,想要通过选择器更改内容。

这意味着:

 <xamlExtensions:BitmapButton 
                                Grid.Column="5"
                                Click="OnBuyButtonClicked" 
                                Command="{Binding BuyCommand}" CommandParameter="{Binding}"
                                Style="{StaticResource BitmapButtonStyle}"
                                DisabledStateBitmap="ms-appx:///Assets/UI/Pages/MainPage/BuyCoinsPopup/disabledButton.png">

                            <xamlExtensions:BitmapButton.Content>
                                <ContentControl ContentTemplateSelector="{StaticResource ButtonSelector}"  Content="{Binding ContentButton}">

                                </ContentControl>

                            </xamlExtensions:BitmapButton.Content>
                        </xamlExtensions:BitmapButton>


<DataTemplate x:Key="EnabledButtonTemplate">
            <TextBlock FontWeight="Bold" FontSize="24" Text="{Binding Path =BuyText}"/>
        </DataTemplate>

        <DataTemplate x:Key="DisabledButtonTemplate" >
            <TextBlock FontWeight="Bold" FontSize="18" Text="{Binding Path =NotAvaliable}"/>
        </DataTemplate>

        <DataTemplate x:Key="TimedButtontemplate" >
            <StackPanel >
                <TextBlock FontWeight="Bold" FontSize="14" Text="{Binding Path= AvaliableInTitle}" HorizontalAlignment="Center" Foreground="Black"/>
                <TextBlock FontWeight="Bold" FontSize="14" Text="{Binding Path = Counter}" HorizontalAlignment="Center" Foreground="Black"/>
            </StackPanel>
        </DataTemplate>
        <helpers1:ButtonDataTemplateSelector x:Key="ButtonSelector"
                          EnabledButtonTemplate="{StaticResource EnabledButtonTemplate}"
                          DisabledButtonTemplate="{StaticResource DisabledButtonTemplate}"
                          TimedButtontemplate="{StaticResource TimedButtontemplate}"/>

查看模型:

public class ContentButton : ObservableObject
    {
        private string _buyText = string.Empty;
        private string _counter = "--:--";
        private ButtonState _buttonState;
        private string _avaliableInTitle = LocalizationManager.GetLocalizedString("AvaliableInTitle");
        private string _notAvaliable = "Not avaliable";

        public string BuyText
        {
            get { return _buyText; }
            set
            {
                _buyText = value;
                RaisePropertyChanged("BuyText");
            }
        }

        public string NotAvaliable
        {
            get { return _notAvaliable; }
            set
            {
                _buyText = value;
                RaisePropertyChanged("NotAvaliable");
            }
        }

        public string Counter
        {
            get { return _counter; }
            set
            {
                _counter = value;
                RaisePropertyChanged("Counter");
            }
        }

        public ButtonState ButtonState
        {
            get
            {
                return _buttonState;
            }
            set
            {
                _buttonState = value;
                RaisePropertyChanged("ButtonState");
            }
        }

        public string AvaliableInTitle
        {
            get { return _avaliableInTitle; }
            set
            {
                _avaliableInTitle = value;
                RaisePropertyChanged("AvaliableInTitle");
            }
        }
    }

public class StorePopupTileItem : TileItem
    {
 private ContentButton _contentButton;
public ContentButton ContentButton
        {
            get
            {
                return _contentButton;
            }
            set
            {
                _contentButton = value;
                RaisePropertyChanged("ContentButton");
            }
        }
    }

我正在绑定StorePopupTileItem;当我更改其中一个属性(AvaliableInTitle,BuyText或其他)时,它在视图上不会改变。

我该如何解决?

由于

添加了:

protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
        {
            if (item != null)
            {
                var itemCur = (ContentButton)item;
                switch (itemCur.ButtonState)
                {
                    case ButtonState.Disabled:
                        return DisabledButtonTemplate;
                    case ButtonState.TimeDisabled:
                        return TimedButtontemplate;
                    case ButtonState.Enabled:
                        return EnabledButtonTemplate;
                }
            }
            return EnabledButtonTemplate;

但如果我只从代码端更改一个字段(f.e. ButtonState),我不知道该怎么办?

在这种情况下,新信息不会绑定。现在,在我想要更新按钮的每一点上,我都需要这样做:

StorePopupTileItem tempItem = TileItem;
                TileItem.ContentButton = new ContentButton
                {
                    BuyText = tempItem.ContentButton.BuyText,
                    ButtonState = ButtonState.Enabled,
                    Counter = tempItem.ContentButton.Counter
}

0 个答案:

没有答案