Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding Variable in List to Textblock INotifyPropertyChanged

I want to Bind a String element in a List to a Textbox. When i click an element the Text gets updated. But when the element in the List changes the Textbox Text doesn't change..

My Code looks like this:

XAML:

   <TextBlock Foreground="White" Name="xTitel" Text="{Binding Titel}" Width="200"  FontSize="36" FontWeight="Bold"/>

OnClick Function:

        foreach (Channel c in App.Connector.ChannelList)
        {
            if (c.StationName == StationName)
            {

                Binding b = new Binding();
                b.Source = c.CurrentTitle;
                xTitel.SetBinding(TextBlock.TextProperty, b);
            }
        }

Channel Definition:

 public class GlobalVariables : INotifyPropertyChanged
{
    public static MediaElement mediaElement;
    private ObservableCollection<Channel> channelList;
    public ObservableCollection<Channel> ChannelList
    {
        get { return channelList; }
        set
        {
            channelList = value;
            NotifyPropertyChanged("ChannelList");
        }
    }

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

EDIT:

What i forgot to say:

In my XAML i have a GridView where it works with Channellist Binding. If I am correct the Textbox should update everytime when my Gridview Updates. This is my Gridview:

<local:VariableGridView
            IsSwipeEnabled="True"
            Background="Transparent"
            x:Name="itemGridView"
            Margin="0,0,0,-3"
            Padding="116,0,40,46"
            SelectionMode="None"
            IsItemClickEnabled="True"
            ItemClick="ItemView_ItemClick" 
            Grid.RowSpan="2"

            >
            <local:VariableGridView.ItemsPanel>
                <ItemsPanelTemplate>
                    <VariableSizedWrapGrid Background="Transparent" Orientation="Vertical" ItemWidth="120" ItemHeight="120"/>
                </ItemsPanelTemplate>
            </local:VariableGridView.ItemsPanel>
            <local:VariableGridView.ItemTemplate>
                <DataTemplate>
                    <Grid Background="{Binding Background}">
                        <StackPanel Orientation="Horizontal" Margin="10,0,0,0">
                            <Image x:Name="CurrentCover" Source="{Binding CurrentCover}"  Width="90" Height="90"/>
                            <StackPanel Orientation="Vertical" HorizontalAlignment="Left" Margin="10,25,10,10">
                                <TextBlock Foreground="White" Text="{Binding Name}" Width="200"  FontSize="24" FontWeight="Bold"/>
                                <TextBlock Foreground="White" Text="{Binding CurrentArtist}" Width="200"  FontSize="12" FontWeight="Bold" />
                                <TextBlock Foreground="White" Text="{Binding CurrentTitle}" Width="200"  FontSize="12" />
                            </StackPanel>                            
                        </StackPanel>
                    </Grid>
                </DataTemplate>
            </local:VariableGridView.ItemTemplate>
        </local:VariableGridView>

And the Channel class:

 public class Channel : INotifyPropertyChanged
{

    public int Width { get; set; }
    public int Height { get; set; }
    public string Logo { get; set; }
    public string StationName { get; set; }
    public string Color { get; set; }
    public SolidColorBrush Background { get; set; }
    //public string Name { get; set; }
    private string name;
    public string Name
    {
        get { return name; }
        set
        {
            name = value;
           NotifyPropertyChanged("Name");
        }
    }

     //..... More Code ...
 }

(The Textboxes in this Gridview do Update. But the other textboxes, where I want to set the Binding over the Code don't update)

like image 251
MrTouch Avatar asked May 08 '26 18:05

MrTouch


1 Answers

Should it be this?

Binding b = new Binding("CurrentTitle");
b.Source = c;
xTitel.SetBinding(TextBlock.TextProperty, b);

Edit to explain a little more: If you bind directly to the string property you will not see the changes because strings are immutable. It'll be set on the original string forever. You have to bind to the object that holds the string property and give the binding a property name (path) to watch for changes to.

like image 61
Jasper Avatar answered May 11 '26 15:05

Jasper



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!