C#/ WPF DependencyProperty Setter未被调用XWidth

时间:2016-07-03 20:06:55

标签: c# wpf dependency-properties

我有一个非常非常简单的课程,它演示了我在一个更复杂的问题中正在努力解决的问题。 类:MyStateControl 两个Dep道具: 状态切换颜色(调用工作和设置器) XWidth旨在将其值传递给Width(未调用Setter)

这不是XWidth解决的真正问题,而是用于演示问题。

我将XWidth链接到comboBox的SelectedItem。只是为了测试我链接到其他对象而其他对象收到了SelectedItems Value。但从来没有我的StateControl。谁知道为什么?

    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;                       
            }
        }
    }
}

和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>

0 个答案:

没有答案