Windows Phone 7 - TextBlock Text binding to ViewModel not returning anythinig

对着背影说爱祢 提交于 2019-12-25 03:32:55

问题


I have built a view model and the listbox binds to the ObervableCollection, but a textbox I am using for a temp url wont bind and return data. I am not getting any errors either on compile or run

ViewModel:

public class HomepageModel:INotifyPropertyChanged
{
    public TextBlock bgrImg{get;set;}
    public ObservableCollection<MenuItem> menu {get; private set;}

    public HomepageModel()
    {
        this.menu = new ObservableCollection<MenuItem>();
    }

    public void Load()
    {
        bgrImg = new TextBlock();
        bgrImg.Text = "/Windows7MobileClient;component/Images/Desert.jpg";
        //bgrImg = ;
        menu.Add(new MenuItem("Feed",""));
        menu.Add(new MenuItem("Messages",""));
        menu.Add(new MenuItem("Media",""));
        menu.Add(new MenuItem("Favourites",""));
        menu.Add(new MenuItem("Freinds",""));
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(String propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (null != handler)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

Homepage.xaml

<controls:PanoramaItem Header="menu">
            <Grid>
                <TextBlock Text="{Binding bgrImg}"/>
                <ListBox x:Name="FirstListBox" Margin="0,0,-12,0" ItemsSource="{Binding menu}" MouseLeftButtonUp="FirstListBox_MouseLeftButtonUp" >
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Margin="0,0,0,17" Width="432">
                                <TextBlock Text="{Binding label}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
            </Grid>
        </controls:PanoramaItem>

I eventual want to use the string for the panorama background image. Sorry if this seems realy obviously simply.

Chris


回答1:


You need to call NotifyPropertyChanged() in your setters for the items you wish to bind to.




回答2:


One more thing I noticed: You are trying to Bind TextBlock to a string Property:

public TextBlock bgrImg{get;set;}
<TextBlock Text="{Binding bgrImg}"/>

Change the type of the property to string:

   public string bgrImg{get;set;}
    <TextBlock Text="{Binding bgrImg}"/>


来源:https://stackoverflow.com/questions/5043873/windows-phone-7-textblock-text-binding-to-viewmodel-not-returning-anythinig

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!