プロパティバインディングについてwpf c#を学んでいます。簡単な wpf アプリケーションを作成します。コードビハインドで、長方形コントロールの幅をプロパティ「PageWidth」にバインドしようとしました。しかし、どういうわけか機能しません (ビューはプロパティの変更を取得しません)。私が達成したいこと: - 長方形の幅はコードビハインドで 100 で初期化されます - ボタン「幅 ++」をクリックすると、長方形の幅は 10 ずつ段階的に増加します。アドバイスをお願いします。私のコードを自由に変更してください。前もって感謝します。
XAML:
<Window x:Class="MyWpfApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Rectangle
Fill="#FF6262BD"
HorizontalAlignment="Left"
Margin="23,24,0,0"
Stroke="Black"
VerticalAlignment="Top"
Width="{Binding Path=PageWidth}"
Height="100" />
<Button
Content="Width++"
HorizontalAlignment="Left"
Margin="35,129,0,0"
VerticalAlignment="Top"
Width="75"
Click="Button_Click" />
</Grid>
xaml.cs:
using System;
using System.Windows;
namespace MyWpfApp
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
PageWidth = 100;
}
private Int32 _pageWidth;
public Int32 PageWidth
{
get
{
return _pageWidth;
}
set
{
if ( _pageWidth != value )
{
_pageWidth = value;
}
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if ( PageWidth <= 200 )
{
PageWidth += 10;
}
}
}
}