c# - 将列表中的变量绑定(bind)到文本 block INotifyPropertyChanged

标签 c# binding inotifypropertychanged

我想将列表中的字符串元素绑定(bind)到文本框。当我单击一个元素时,文本会更新。但是当列表中的元素发生变化时,文本框文本不会改变..

我的代码是这样的:

XAML:

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

OnClick 函数:

        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 定义:

 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));
        }
    }
}

编辑:

我忘了说:

在我的 XAML 中,我有一个 GridView,它与 Channellist 绑定(bind)一起使用。如果我是正确的,那么每次我的 Gridview 更新时,文本框都应该更新。这是我的 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>

和 Channel 类:

 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 ...
 }

(此 Gridview 中的文本框会更新。但我想在代码上设置绑定(bind)的其他文本框不会更新)

最佳答案

应该是这个吧?

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

编辑解释更多一点:如果您直接绑定(bind)到字符串属性,您将看不到更改,因为字符串是不可变的。它将永远设置在原始字符串上。您必须绑定(bind)到包含字符串属性的对象,并为绑定(bind)提供属性名称(路径)以监视更改。

关于c# - 将列表中的变量绑定(bind)到文本 block INotifyPropertyChanged,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12407732/

相关文章:

c# - 我可以防止 SQL 注入(inject)吗?

WPF 数据网格 : CanContentScroll property causing odd behavior

Java 将 bean 绑定(bind)到 String 空属性

.net - WPF/Silverlight 世界之外的 INotifyPropertyChanged 用法

wpf - 使用 linq 表达式的类型安全 NotifyPropertyChanged

c# - svg2png 性能

c# - Lucene docID 可靠性

WCF 多重绑定(bind)端口号

vb.net - VB 传递参数值 ByRef 在方法退出之前不会改变值?

c# - MonoGame GetData 实现异常