WPF UserControl:绑定无法正常工作,更新相关属性时出现问题,并且TextBox无法编辑

时间:2018-12-05 04:42:02

标签: c# wpf xaml binding user-controls

我知道这里已经有人提出类似的要求,并且我已经阅读了这些答案……但我认为这些都与我的情况无关。

因此,我已经开发了WPF应用程序,我想开始开发自己的UserControls,而不是将所有XAML控件直接放在Pages中,以便在整个应用程序中使用现成的UI本身。

现在,这是我的UserControl XAML的相关代码:

<UserControl x:Class="DigitalColorimeter.Controls.ColorViewer"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:DigitalColorimeter.Controls"
         mc:Ignorable="d"
         d:DesignHeight="155" d:DesignWidth="125">
  <Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <TextBlock x:Name="TestoTitle" Text="{Binding Title, Mode=OneWay}" FontSize="16" FontWeight="Bold"/>
    <Rectangle x:Name="AreaColore" Grid.Row="1" Fill="{Binding ColorBrush, Mode=OneWay}"/>
    // Other controls...
    <TextBlock x:Name="TestoColorString" Grid.Row="5" Grid.ColumnSpan="3" Text="#FF223344" HorizontalAlignment="Left" VerticalAlignment="Center" FontWeight="SemiBold" Margin="0,4,0,0" TextWrapping="Wrap"/>
  </Grid>
</UserControl>

在这里,我们改为使用UserControl的类:

public partial class ColorViewer : UserControl
{
    public ColorViewer()
    {
        InitializeComponent();
    }

    public Color Color
    {
        get { return (Color)GetValue(ColorProperty); }
        set
        {
            SetValue(ColorProperty, value);

            Debug.WriteLine("--- COLOR changed!");

            // Updating other properties:
            ColorBrush = new SolidColorBrush(value);
            UpdateColorProperties(value);
        }
    }
    public static readonly DependencyProperty ColorProperty =
        DependencyProperty.Register("Color", typeof(Color), typeof(ColorViewer), null);


    public SolidColorBrush ColorBrush
    {
        get { return (SolidColorBrush)GetValue(ColorBrushProperty); }
        set { SetValue(ColorBrushProperty, value); }
    }
    public static readonly DependencyProperty ColorBrushProperty =
        DependencyProperty.Register("ColorBrush", typeof(SolidColorBrush), typeof(ColorViewer), null);

    public String Title
    {
        get { return (String)GetValue(TitleProperty); }
        set 
            {
               SetValue(TitleProperty, value);
               UpdateTitle(value);
            }
    }
    public static readonly DependencyProperty TitleProperty =
        DependencyProperty.Register("Title", typeof(String), typeof(ColorViewer), null);

    private void UpdateTitle(string s)
    {
        if (s == "")
        {
            TestoTitle.Visibility = Visibility.Collapsed;
        }
        else
        {
            TestoTitle.Visibility = Visibility.Visible;
        }
    }


    private void UpdateColorProperties(Color c)
    {
        Debug.WriteLine("UPDATE COLOR | color: " + c.ToString());

        AreaColore.Fill = new SolidColorBrush(c);
        ColorBrush = new SolidColorBrush(c);
        TestoColorString.Text = c.ToString();
    }
}

...这是我对其进行全部测试的第一页(UserControl称为“ ColorViewer”,ExtendedWPF是Nuget库...):

<Page x:Class="DigitalColorimeter.View.PaginaSelettoreControl"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  xmlns:local="clr-namespace:DigitalColorimeter.View"
  xmlns:ExtendedWPF="http://schemas.xceed.com/wpf/xaml/toolkit"
  xmlns:controls="clr-namespace:DigitalColorimeter.Controls"
  mc:Ignorable="d" 
  d:DesignHeight="300" d:DesignWidth="300"
  Title="Picker control page">

<ScrollViewer VerticalScrollBarVisibility="Auto">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>

        <TextBox Grid.ColumnSpan="2" Text="{Binding TestoTest, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Margin="5" />

        <ExtendedWPF:ColorCanvas Grid.Row="1" x:Name="SelettoreControl" SelectedColor="{Binding ColorSelected, Mode=TwoWay}" Margin="25" HorizontalAlignment="Center"/>

        // Other controls…

        <controls:ColorViewer Grid.Row="1" Grid.Column="1" Width="175" Color="{Binding ColorSelected, Mode=OneWay}" Title="{Binding TestoTest, Mode=OneWay}" Margin="15"/>
    </Grid>
</ScrollViewer>

所以,这是我的主意:我希望UserControl的“ Color”属性甚至更新“ TestoColorString”文本块(当然还有颜色的字符串)和“ AreaColore”矩形中显示的颜色。

问题在于:

  • 当ColorSelected在AppViewModel中发生更改时(我测试了它),UserControl不会收到它

  • 我不知道确切的正确方式来更新依赖于它们的其他依赖项属性(您可以看到在“ Color”属性的set方法的backcode中调用了一些函数……但是我我不确定这样是否好。

  • 为了测试UserControl的“ Title”属性而在页面中插入了TextBox,不允许修改其文本! 我唯一可以做的更改就是插入空格或删除字符! 无论是否绑定到某物,它都会这样做!

希望我已经很好地解释了……我完全被困住了。

感谢您的关注,如果您需要更多信息,请发表评论。

最诚挚的问候

0 个答案:

没有答案