3

リスト内の文字列要素をテキストボックスにバインドしたい。要素をクリックすると、テキストが更新されます。ただし、リスト内の要素が変更されても、テキストボックスのテキストは変更されません。

私のコードは次のようになります:

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

チャネル定義:

 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には、ChannellistBindingで動作するGridViewがあります。正しければ、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のテキストボックスは更新されます。ただし、コードのバインドを設定したい他のテキストボックスは更新されません)

4

1 に答える 1

1

これでいいの?

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

もう少し説明するために編集します。文字列プロパティに直接バインドすると、文字列は不変であるため、変更は表示されません。元の文字列に永久に設定されます。文字列プロパティを保持するオブジェクトにバインドし、変更を監視するためのプロパティ名(パス)をバインドに指定する必要があります。

于 2012-09-13T15:08:40.187 に答える