WPF-TextBox在多重绑定中不会响应PropertyChanged

时间:2019-03-30 13:17:18

标签: c# wpf binding multibinding

我有一个文本框,应显示颜色的十六进制值。应用可以处于两种状态:

1)在编辑模式(IsEdit == true)中,文本框绑定到我的模型属性,并且

2)不处于编辑模式时(IsEdit == false)文本框绑定到滑块 代表RGB颜色。使用滑块更改每个颜色值,文本框中的十六进制值也会更改,反之亦然。文本框也绑定到我的模型属性,以便以后保存更改。

问题:当应用程序处于编辑模式时,更改文本框文本属性会手动触发转换器并将文本值发送给模型和滑块,但是当我更改滑块时,文本框文本会发生更改,但不会发送数据模拟。为什么?!我已经尽力想了一切。更改绑定模式,notifyontargetupdated ...应该可以正常工作。有人可以解释一下。

我试图简化,所以如果我错过任何重要的事情,请告诉我。

XAML代码:

<Rectangle Grid.Column="0" Width="120" Height="120" Stroke="Black" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="5,0,0,0" Fill="{Binding ElementName=tbxHexColor, Path=Text, Converter={StaticResource HexToColorConverter}}"/>
<TextBox x:Name="tbxHexColor" CharacterCasing="Upper">
    <TextBox.Style>
        <Style TargetType="TextBox">
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsEdit}" Value="False">
                    <Setter Property="Text" Value="{Binding SelectedColor.RGBHex}"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding IsEdit}" Value="True">
                    <Setter Property="Text">
                    <Setter.Value>
                        <MultiBinding Converter="{StaticResource SlidersToHexConverter}" UpdateSourceTrigger="PropertyChanged">
                            <Binding ElementName="sliderRed" Path="Value"/>
                            <Binding ElementName="sliderGreen" Path="Value"/>
                            <Binding ElementName="sliderBlue" Path="Value"/>
                            <Binding Path="RGBHelper"/>
                        </MultiBinding>
                    </Setter.Value>
                    </Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBox.Style>
</TextBox>
<Slider x:Name="sliderRed" Grid.Column="1" Grid.Row="0" Margin="0,5,0,3" Maximum="255">
    <Slider.Style>
        <Style TargetType="Slider">
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsEdit}" Value="False">
                    <Setter Property="Value" Value="{Binding SelectedColor.RGBHex, Converter={StaticResource HexToRGBConverter}, ConverterParameter=red, FallbackValue=0}"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding IsEdit}" Value="True">
                    <Setter Property="Value" Value="{Binding SelectedColor.RGBHex, Converter={StaticResource HexToRGBConverter}, ConverterParameter=red, Mode=OneTime}"/> 
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Slider.Style>
</Slider>
<Slider x:Name="sliderGreen" Grid.Column="1" Grid.Row="1" Margin="0,3" Maximum="255">
    <Slider.Style>
        <Style TargetType="Slider">
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsEdit}" Value="False">
                    <Setter Property="Value" Value="{Binding SelectedColor.RGBHex, Converter={StaticResource HexToRGBConverter}, ConverterParameter=green, FallbackValue=0}"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding IsEdit}" Value="True">
                    <Setter Property="Value" Value="{Binding SelectedColor.RGBHex, Converter={StaticResource HexToRGBConverter}, ConverterParameter=green, Mode=OneTime}"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Slider.Style>
</Slider>
<Slider x:Name="sliderBlue" Grid.Column="1" Grid.Row="2" Margin="0,3" Maximum="255">
    <Slider.Style>
        <Style TargetType="Slider">
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsEdit}" Value="False">
                    <Setter Property="Value" Value="{Binding SelectedColor.RGBHex, Converter={StaticResource HexToRGBConverter}, ConverterParameter=blue, FallbackValue=0}"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding IsEdit}" Value="True">
                    <Setter Property="Value" Value="{Binding SelectedColor.RGBHex, Converter={StaticResource HexToRGBConverter}, ConverterParameter=blue, Mode=OneTime}"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Slider.Style>
</Slider>

转换器:

public class HexToColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {    
        string input = "#" + value as string;
        if (string.IsNullOrEmpty(input) || input == "#" || input.Length < 7)
        {
            input = "#00FFFFFF";
        }

        return new SolidColorBrush((Color)ColorConverter.ConvertFromString(input));            
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}



public class SlidersToHexConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        string hex = "";
        for (int i = 0; i < 3; i++)
        {
            int itemInt = System.Convert.ToInt32(values[i]);
            if (itemInt != 0)
            {
                string a = itemInt.ToString("X");
                if (a.Length == 1)
                {
                    hex += "0" + a;
                }
                else
                {
                    hex += a;
                }
            }
            else
            {
                hex += "00";
            }
        }

        return hex;                                   
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        object[] sliderValues = new object[4] { 0, 0, 0, "" };

        if (value.ToString().Length == 6)
        {
            string r = value.ToString().Substring(0, 2);
            string g = value.ToString().Substring(2, 2);
            string b = value.ToString().Substring(4, 2);

            double rr = int.Parse(r, NumberStyles.HexNumber);
            double gg = int.Parse(g, NumberStyles.HexNumber);
            double bb = int.Parse(b, NumberStyles.HexNumber);

            sliderValues[0] = (object)rr;
            sliderValues[1] = (object)gg;
            sliderValues[2] = (object)bb;
        }

        sliderValues[3] = value.ToString();

        return sliderValues;
    }
}



public class HexToRGBConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null)
        {
            return 0;
        }
        string hex = value.ToString();
        string colorHex = "0";
        switch (parameter.ToString())
        {
            case "red":
                colorHex = hex.Substring(0, 2);
                break;
            case "green":
                colorHex = hex.Substring(2, 2);
                break;
            case "blue":
                colorHex = hex.Substring(4, 2);
                break;
            default:
                break;
        }

        return int.Parse(colorHex, NumberStyles.HexNumber);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

尝试了XAML的MultiBinding部分中的所有内容。更改:NotifyOnSourceUpdated,NotifyOnTargetUpdated,模式,UpdateSourceTrigger ...什么都行不通。

0 个答案:

没有答案