M
mywatermelon
Guest
XAML:
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<ScrollViewer>
<ItemsControl ItemsSource="{Binding TestModelCollection}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Foreground="#575757" Grid.Column="1" FontSize="15" TextAlignment="Right">
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} / {1}" NotifyOnSourceUpdated="True" UpdateSourceTrigger="PropertyChanged">
<Binding Path="CurrentValue"></Binding>
<Binding Path="MaxValue"></Binding>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
<Button Grid.Row="1" Click="Button_Click"></Button>
</Grid>
</Window>
Code-behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.ComponentModel;
namespace WpfApp1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window,INotifyPropertyChanged
{
public MainWindow()
{
InitializeComponent();
TestModelCollection = new ObservableCollection<TestModel>();
TestModelCollection.Add(new TestModel() { CurrentValue = 0, MaxValue = 100 });
TestModelCollection.Add(new TestModel() { CurrentValue = 0, MaxValue = 100 });
TestModelCollection.Add(new TestModel() { CurrentValue = 0, MaxValue = 100 });
this.DataContext = this;
}
public ObservableCollection<TestModel> TestModelCollection { get; set; }
public class TestModel
{
public int MaxValue { get; set; }
public int CurrentValue { get; set; }
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
TestModelCollection[0].CurrentValue++;
OnPropertyChanged("TestModelCollection");
}
}
}
After I clicked the Button,the TestModelCollection[0].CurrentValue changed but the UI not.
I have used the INotifyPropertyChanged interface yet but why it does not work?
Please help me, thank you.
Continue reading...
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<ScrollViewer>
<ItemsControl ItemsSource="{Binding TestModelCollection}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Foreground="#575757" Grid.Column="1" FontSize="15" TextAlignment="Right">
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} / {1}" NotifyOnSourceUpdated="True" UpdateSourceTrigger="PropertyChanged">
<Binding Path="CurrentValue"></Binding>
<Binding Path="MaxValue"></Binding>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
<Button Grid.Row="1" Click="Button_Click"></Button>
</Grid>
</Window>
Code-behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.ComponentModel;
namespace WpfApp1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window,INotifyPropertyChanged
{
public MainWindow()
{
InitializeComponent();
TestModelCollection = new ObservableCollection<TestModel>();
TestModelCollection.Add(new TestModel() { CurrentValue = 0, MaxValue = 100 });
TestModelCollection.Add(new TestModel() { CurrentValue = 0, MaxValue = 100 });
TestModelCollection.Add(new TestModel() { CurrentValue = 0, MaxValue = 100 });
this.DataContext = this;
}
public ObservableCollection<TestModel> TestModelCollection { get; set; }
public class TestModel
{
public int MaxValue { get; set; }
public int CurrentValue { get; set; }
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
TestModelCollection[0].CurrentValue++;
OnPropertyChanged("TestModelCollection");
}
}
}
After I clicked the Button,the TestModelCollection[0].CurrentValue changed but the UI not.
I have used the INotifyPropertyChanged interface yet but why it does not work?
Please help me, thank you.
Continue reading...