Почему не работает Биндинг?
Сам код формы:
Код:
<UserControl x:Class="SilverlightApplication1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:toolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit"
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
x:Name="Dima"
Width="1220" Height="960"
MouseEnter="User_Control_MouseEnter" >
<Grid x:Name="rootCanvas"
Width="640"
Height="480"
Background="Gray"
>
<Rectangle Height="100" HorizontalAlignment="Left" Margin="118,166,0,0" Name="rectangle1" Stroke="Black" StrokeThickness="1" VerticalAlignment="Top" Width="200" Fill="#5C00FF00" MouseEnter="mouse_rectangle" />
<TextBox Height="{Binding ElementName=Dima, Path=test}" HorizontalAlignment="Left" Margin="444,167,0,0" Name="textBox1" VerticalAlignment="Top" Width="151"/>
</Grid>
</UserControl>
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:toolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit"
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
x:Name="Dima"
Width="1220" Height="960"
MouseEnter="User_Control_MouseEnter" >
<Grid x:Name="rootCanvas"
Width="640"
Height="480"
Background="Gray"
>
<Rectangle Height="100" HorizontalAlignment="Left" Margin="118,166,0,0" Name="rectangle1" Stroke="Black" StrokeThickness="1" VerticalAlignment="Top" Width="200" Fill="#5C00FF00" MouseEnter="mouse_rectangle" />
<TextBox Height="{Binding ElementName=Dima, Path=test}" HorizontalAlignment="Left" Margin="444,167,0,0" Name="textBox1" VerticalAlignment="Top" Width="151"/>
</Grid>
</UserControl>
Код:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace SilverlightApplication1
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
test = 233;
}
private void User_Control_MouseEnter(object sender, MouseEventArgs e)
{
textBox1.Background = new SolidColorBrush(Colors.Blue);
textBox1.Text = e.GetPosition(null).X.ToString();
}
private void mouse_rectangle(object sender, MouseEventArgs e)
{
}
int test;
public int Test
{
get { return test; }
set
{
// test = 33;
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace SilverlightApplication1
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
test = 233;
}
private void User_Control_MouseEnter(object sender, MouseEventArgs e)
{
textBox1.Background = new SolidColorBrush(Colors.Blue);
textBox1.Text = e.GetPosition(null).X.ToString();
}
private void mouse_rectangle(object sender, MouseEventArgs e)
{
}
int test;
public int Test
{
get { return test; }
set
{
// test = 33;
}
}
}
}
Код:
<TextBox Height="{Binding ElementName=Dima, Path=Test}" HorizontalAlignment="Left" Margin="444,167,0,0" Name="textBox1" VerticalAlignment="Top" Width="151"/>
Еще раз поясню идею - хочу через биндинг подвязать движение мыши к изменению размера текстового поля.
Код:
class Model : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
int _Test;
public int Test
{
get
{
return _Test;
}
set
{
if (_Test != value)
{
_Test = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("Test");
}
}
}
}
}
{
public event PropertyChangedEventHandler PropertyChanged;
int _Test;
public int Test
{
get
{
return _Test;
}
set
{
if (_Test != value)
{
_Test = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("Test");
}
}
}
}
}
типа так.
Как в мой код вставить данный ивент?
: INotifyPropertyChanged
он запросит имплементацию - сделайте ее (из квадратика под именем итерфейса)
Для "подьема" эвента можно написать функцию
Код:
private void PropChange(string PropName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(PropName));
}
}
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(PropName));
}
}
и вызывать ее из каждого сеттера свойств.
Я пока сделал так:
Код:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace SilverlightApplication1
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
test = 233;
}
private void User_Control_MouseEnter(object sender, MouseEventArgs e)
{
textBox1.Background = new SolidColorBrush(Colors.Blue);
// textBox1.Text = e.GetPosition(null).X.ToString();
textBox1.Text = test.ToString();
}
private void mouse_rectangle(object sender, MouseEventArgs e)
{
// MessageBox.Show(Test);
}
private void PropChange(string PropName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(PropName));
}
}
class DataContent : INotifyPropertyChanged
{
int test;
public int Test
{
get { return test; }
set
{
test = 33;
}
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace SilverlightApplication1
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
test = 233;
}
private void User_Control_MouseEnter(object sender, MouseEventArgs e)
{
textBox1.Background = new SolidColorBrush(Colors.Blue);
// textBox1.Text = e.GetPosition(null).X.ToString();
textBox1.Text = test.ToString();
}
private void mouse_rectangle(object sender, MouseEventArgs e)
{
// MessageBox.Show(Test);
}
private void PropChange(string PropName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(PropName));
}
}
class DataContent : INotifyPropertyChanged
{
int test;
public int Test
{
get { return test; }
set
{
test = 33;
}
}
}
}
}
Error 1 The type or namespace name 'INotifyPropertyChanged' could not be found (are you missing a using directive or an assembly reference?)
Но там тоже должно быть что-то подобное.
подключите сборку, и добавьте using System.ComponentModel;
Код:
class DataContent : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
int test;
public int Test
{
get { return test; }
set
{
test = value;
PropChange("Test")
}
}
private void PropChange(string PropName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(PropName));
}
}
}
{
public event PropertyChangedEventHandler PropertyChanged;
int test;
public int Test
{
get { return test; }
set
{
test = value;
PropChange("Test")
}
}
private void PropChange(string PropName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(PropName));
}
}
}
путем вызова эвента - а на эвент подписан биндинг.
по строке с именем свойства он знает, какой get вызывать.
Если вы ошибетесь в стоке - забудете изменить имя свойства - то компилятор вам ничего не скажет, а работать не будет.