更改ItemTemplate中列的值

时间:2014-12-12 13:38:43

标签: c# wpf windows-phone

我有这样的事情:

<ListBox Margin="10, 10, 10, 1" Visibility="Visible" ItemContainerStyle="{StaticResource Remind_list}" x:Name="Reminders_list" Background="Transparent" Height="auto">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid Width="440">
                <Grid.Background>
                    <ImageBrush Stretch="Fill" ImageSource="/Images/bg.png"/>
                </Grid.Background>
                <Grid.RowDefinitions>
                    <RowDefinition Height="60"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>
                <Image Grid.Row="0" Grid.Column="1" Source="/Images/check.png" Stretch="Fill" Visibility="{Binding Checked}"/>
                <TextBlock Grid.Row="0" Grid.Column="0"  Tap="RemindName_Tap" HorizontalAlignment="Left" Text="{Binding Title}" TextWrapping="Wrap" Padding="10,2,0,0" Foreground="White" Height="40"  Margin="2,0,0,0" FontSize="24" Width="365"/>
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>

这是一个绑定列表框。

如何更改列的值和&#34; Grid.Column&#34;在C#中的图像和文本块?

1 个答案:

答案 0 :(得分:0)

创建第二个视图并在它们之间切换。这是切换示例

public class ViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private Visibility _visibility1;
    public Visibility Visibility1
    {
        get { return _visibility1; }
        set
        {
            _visibility1 = value;
            OnPropertyChanged();
        }
    }

    private Visibility _visibility2;
    public Visibility Visibility2
    {
        get { return _visibility2; }
        set
        {
            _visibility2 = value;
            OnPropertyChanged();
        }
    }


    public void OnPropertyChanged([CallerMemberName] string property = "")
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(property));
    }
}

public partial class MainWindow : Window
{
    private ViewModel _vm = new ViewModel() { Visibility1 = Visibility.Visible, Visibility2 = Visibility.Collapsed };

    public MainWindow()
    {
        InitializeComponent();
        DataContext = _vm;
    }

    private void CheckBox_Checked(object sender, RoutedEventArgs e)
    {
        _vm.Visibility1 = Visibility.Collapsed;
        _vm.Visibility2 = Visibility.Visible;
    }

    private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
    {
        _vm.Visibility1 = Visibility.Visible;
        _vm.Visibility2 = Visibility.Collapsed;
    }
}

XAML

<StackPanel>
    <CheckBox Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked"/>
    <TextBlock Text="ListBox1" Visibility="{Binding Visibility1}"/>
    <TextBlock Text="ListBox2" Visibility="{Binding Visibility2}"/>
</StackPanel>

TextBlock是你的两个ListBox es。 CheckBox(以及后面的一些代码)是模拟VisibilityX属性的更改,这通常在处理某些CommandSwapColumns时会发生。

可以使用转换器绑定到一个bool属性。