I have a very very simple class that demonstrates what I am struggling with in a more complex problem. Class: MyStateControl Two Dep Props: State toggles color (works and setter is called) XWidth intended to pass its value to Width (Setter is not called)
This is not the real problem that XWidth solves but is used to demonstrate issue.
I am linking XWidth to the SelectedItem of a comboBox. Just to test I linked to other objects and the other objects received the SelectedItems Value. But never My StateControl. Anyone know why?
using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace TestDepProp
{
public partial class MainWindow : Window
{
private ObservableCollection<double> lstWidths = new ObservableCollection<double>() { 25, 50, 75 };
public MainWindow()
{
InitializeComponent();
listBox.DataContext = lstWidths;
listBox.ItemsSource = lstWidths;
}
public ObservableCollection<double> LstWidths
{
get { return lstWidths;}
set{ lstWidths = value; }
}
private void button_Click(object sender, RoutedEventArgs e)
{
stateButton.State = !stateButton.State;
}
}
public class MyStateControl : Button
{
public MyStateControl() : base() { }
public static readonly DependencyProperty StateProperty = DependencyProperty.Register(
"State", typeof(Boolean), typeof(MyStateControl), new PropertyMetadata(false));
public Boolean State
{
get { return (Boolean)this.GetValue(StateProperty); }
set
{
this.SetValue(StateProperty, value);
if (value == true)
this.Background = Brushes.Green;
else
this.Background = Brushes.Red;
}
}
public static readonly DependencyProperty XWidthProperty = DependencyProperty.Register(
"XWidth", typeof(double), typeof(MyStateControl), new PropertyMetadata(0.0));
public double XWidth
{
get { return (double)this.GetValue(XWidthProperty); }
set
{
this.SetValue(XWidthProperty, value);
this.Width = (double)value;
}
}
}
}
And the Xaml:
<Window x:Class="TestDepProp.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:TestDepProp"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button x:Name="button" Width="{Binding SelectedValue, ElementName=listBox}" Content="Button" HorizontalAlignment="Left" Margin="326,54,0,0" VerticalAlignment="Top" Click="button_Click"/>
<ListBox x:Name="listBox" HorizontalAlignment="Left" Height="194" Margin="54,34,0,0" VerticalAlignment="Top" Width="100"
ItemsSource="{Binding lstWidths}"/>
<local:MyStateControl XWidth ="{Binding SelectedValue, ElementName=listBox}" x:Name="stateButton" Content="MyStateControl" HorizontalAlignment="Left" Margin="292,156,0,0" VerticalAlignment="Top" Height="72" Width="130" State="True" />
<Label x:Name="label" Content="{Binding SelectedValue, ElementName=listBox}" HorizontalAlignment="Left" Margin="187,54,0,0" VerticalAlignment="Top" Height="23" RenderTransformOrigin="0.5,0.5" Width="52">
</Label>
<Label x:Name="label1" Content="{Binding XWidth, ElementName=stateButton}" HorizontalAlignment="Left" Margin="188,156,0,0" VerticalAlignment="Top"/>
</Grid>
</Window>