Je suis en train de mieux comprendre le mécanisme de liaison Silverlights et j'ai donc créé un programme simple qui va changer l'épaisseur de bordure d'une liste sur la pression d'un bouton. Cependant, cela ne fonctionne pas et je ne peux pas comprendre ce que je fais mal. Des idées?Silverlight - liaison pour contrôler borderthickness
XAML:
<Grid x:Name="LayoutRoot" Background="White">
<ListBox Height="100" HorizontalAlignment="Left" Margin="134,102,0,0" Name="listBox1" VerticalAlignment="Top" Width="120" BorderThickness="{Binding TheThickness, Mode=TwoWay}" />
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="276,36,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
</Grid>
code:
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
namespace SilverlightApplication4
{
public partial class MainPage : UserControl
{
private TestClass testInst = new TestClass();
public MainPage()
{
InitializeComponent();
listBox1.DataContext = testInst;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
testInst.TheThickness = 10;
}
}
public class TestClass
{
private int theThickness = 5;
public int TheThickness
{
get { return theThickness; }
set
{
theThickness = value;
NotifyPropertyChanged("TheThickness");
}
}
public event PropertyChangedEventHandler PropertyChanged;
// NotifyPropertyChanged will raise the PropertyChanged event, passing the source property that is being updated.
private void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
Merci, je suis en train de le faire fonctionner à présent. L'éditeur VS ne reconnaît pas d'où proviennent GetValue et SetValue ... – Calanus
Oui, désolé, je viens de couper un exemple réel d'un code que j'avais déjà. Les trucs SetValue et GetValue supposent que l'objet est dérivé d'un DependencyObject, vous n'en avez pas réellement besoin simplement de remplacer votre utilisation de 'int' par' Thickness' et de remplacer votre affectation de valeur par défaut. Je vais modifier la réponse. – AnthonyWJones
Merci! Tu es une star! – Calanus