Wpf 绑定控件';将属性绑定到变量,将变量绑定到另一个控件

Wpf 绑定控件';将属性绑定到变量,将变量绑定到另一个控件,wpf,binding,Wpf,Binding,从标题上很难理解一些东西,但对我来说也很难解释,但我会努力的 我有自定义控件来选择颜色。 自定义控件具有名为SelectedColor的属性: /// <summary> /// SelectedColor property backing ReadOnly DependencyProperty. /// </summary> private static readonly DependencyPropertyKey SelectedColorPropertyKey

从标题上很难理解一些东西,但对我来说也很难解释,但我会努力的

我有自定义控件来选择颜色。
自定义控件具有名为SelectedColor的
属性

/// <summary>
/// SelectedColor property backing ReadOnly DependencyProperty.
/// </summary>
private static readonly DependencyPropertyKey SelectedColorPropertyKey
    = DependencyProperty.RegisterReadOnly("SelectedColor", typeof(Color), typeof(ImageColorPicker)
    , new FrameworkPropertyMetadata(Colors.Transparent
        , FrameworkPropertyMetadataOptions.AffectsRender));
/// <summary>
/// Gets or sets the color selected.
/// </summary>
/// <value>The color selected.</value>
public Color SelectedColor
{
    get { return (Color)GetValue(SelectedColorPropertyKey.DependencyProperty); }
}
现在,我想将TabColor绑定到我的样式上的属性:

<Style TargetType="{x:Type MetroStyle:MetroTabItem}">
    <Setter Property="OverridesDefaultStyle" Value="True" />
    <Setter Property="FocusVisualStyle" Value="{x:Null}" />
    <Setter Property="Header" Value="{Binding TabTitle}" />
    <Setter Property="Foreground" Value="{Binding TabColor}" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type MetroStyle:MetroTabItem}">
                <Grid Height="26" Background="{TemplateBinding Background}">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>
                    <ContentPresenter Margin="5,0" HorizontalAlignment="Left" VerticalAlignment="Center" ContentSource="Header">
                        <ContentPresenter.Resources>
                            <Style TargetType="TextBlock">
                                <Setter Property="Foreground" Value="{Binding Foreground, RelativeSource={RelativeSource AncestorType={x:Type MetroStyle:MetroTabItem}}}" />
                                <Setter Property="HorizontalAlignment" Value="Center" />
                                <Setter Property="VerticalAlignment" Value="Center" />
                            </Style>
                        </ContentPresenter.Resources>
                    </ContentPresenter>
                    <StackPanel Grid.Column="1" Height="16" Margin="0,0,1,0" HorizontalAlignment="Center" VerticalAlignment="Center" Orientation="Horizontal">
                        <Button Width="16" Click="ShowHide_Click" Content="&#x1f315;" Style="{StaticResource CustomizedMetroTabItemButton}" ToolTip="Hide" Visibility="{Binding WindowsVisible, Converter={StaticResource BooleanToVisibilityConverter}}" />
                        <Button Width="16" Click="ShowHide_Click" Content="&#x1f311;" Style="{StaticResource CustomizedMetroTabItemButton}" ToolTip="Show" Visibility="{Binding WindowsVisible, Converter={StaticResource InverseBooleanToVisibilityConverter}}" />
                        <Button Width="16" Click="Clear_Click" Content="&#xE107;" Style="{StaticResource CustomizedMetroTabItemButton}" ToolTip="Clear" />
                        <ToggleButton Width="16" x:Name="Edit" Content="&#xE104;" Style="{StaticResource CustomizedMetroTabItemToggleButton}" ToolTip="Edit" />
                        <Popup IsOpen="{Binding IsChecked, Mode=TwoWay, ElementName=Edit}" StaysOpen="False" PlacementTarget="{Binding ElementName=Edit}" Placement="Left" VerticalOffset="{Binding ActualHeight, ElementName=Edit}" HorizontalOffset="{Binding Width, ElementName=Edit}" PopupAnimation="Slide">
                            <StackPanel>
                                <local:ImageColorPicker x:Name="ColorPicker" Source="Images/ColorWheel.png" HorizontalAlignment="Center" Width="100" Height="100"/>
                            </StackPanel>
                        </Popup>
                    </StackPanel>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsSelected" Value="False">
                        <Setter Property="Background" Value="#EFEFF2" />
                    </Trigger>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Background" Value="#FFFFFF" />
                        <Setter Property="Foreground" Value="{Binding TabColor}" />
                    </Trigger>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter Property="Background" Value="{Binding TabColor}" />
                        <Setter Property="Foreground" Value="#FFFFFF" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
EDIT2:

EDIT3:
一张新的图片(也许会更容易理解)

我相信您所需要做的就是在选定的颜色上设置绑定,例如:

<local:ImageColorPicker x:Name="ColorPicker" Source="Images/ColorWheel.png" HorizontalAlignment="Center" Width="100" Height="100" SelectedColor="{Binding Path=TabColor, Converter={StaticResource ColorToBrushConverter}}/>
这样,对“TabColor”属性的任何更改都不会传播到ColorPicker,但来自ColorPicker的更改将设置“TabColor”属性。作为参考,其他模式有:TwoWay(默认)、OneTime(仅在初始化时获取默认值)和OneWayToSource(类似于OneWay,但它只更改源,从不获取更新)

当我提到“不寻常”依赖属性时,我指的是“SelectedColor”DP的样式。当然,TabColor将不是DP:)

最后,我相信您可以像前面所描述的那样绑定Value属性。你试过了吗?绑定的唯一限制是必须在DP上执行

更新

我无法让ColorPicker更新视图模型。我读到的所有东西都表明它应该能够,但我没能弄明白。如果这是一个重要的要求,我建议从零开始,开始工作,然后继续

也就是说,我确实更新了颜色。尝试将XAML更改为:

<TabItem Header="Test" Foreground="{Binding ElementName=ColorPicker, Path=SelectedColor, Converter={StaticResource ColorToBrushConverter}, Mode=OneWay}">
    <Grid>
       <local:ImageColorPicker x:Name="ColorPicker" Source="ColorWheel.png" HorizontalAlignment="Center"/>
    </Grid>
</TabItem>

出于某种原因,我不得不重建几次,以使这项工作。再次,我很抱歉,我无法使原始请求生效

更新2

显然,您需要做的就是将“bindstwaybydefault”添加到元数据选项中。这将导致调用“ConvertBack”函数,因为您正在更新ViewModel


除了ImageColorPicker之外,您可能不需要对所有控件进行“单向”绑定,这样它们就不会意外地设置颜色,但一般来说双向绑定就可以了。

我认为您无法将笔刷绑定到颜色。最多可以将其设置为SolidColorBrush。如果你能让这两个人匹配起来,这行得通吗?另外,“SelectedColor”绑定在哪里?@LordTakkera,我正在使用转换器将颜色转换为SolidColorBrush。我没有绑定SelectedColor,因为我不知道在哪里绑定它,也不知道它将如何将SelectedColor绑定到TabColor(如果TabColor为红色,则SelectedColor将为红色),我想要的正好相反。还是我遗漏了什么?另外,TabColor不能是
DependencyProperty
,因为我没有
GetValue
SetValue
函数(该类不是从控件继承的,它是一个视图模型),因为它说“从树中绑定属性”,我的意思是我不能执行以下操作:
@Ron,请查看我的更新,了解我对您评论的看法,并让我知道您的想法!是的,我尝试了上面的方法,但它没有做任何事情,只是在树中的某个元素的前景中绑定时起作用,尽管我不想这样做。选择的颜色是
dependencProperty
,正如你在我的帖子中看到的,但它仍然不起作用(顺便说一句,它从
dependencPropertyKey
更改为
dependencProperty
,但仍然不起作用)
<local:ImageColorPicker x:Name="ColorPicker" Source="Images/ColorWheel.png" HorizontalAlignment="Center" Width="100" Height="100" SelectedColor="{Binding Path=TabColor, Converter={StaticResource ColorToBrushConverter}}/>
SelectedColor="{Binding Path=TabColor, Converter={StaticResource ColorToBrushConverter, Mode=OneWay}}
<TabItem Header="Test" Foreground="{Binding ElementName=ColorPicker, Path=SelectedColor, Converter={StaticResource ColorToBrushConverter}, Mode=OneWay}">
    <Grid>
       <local:ImageColorPicker x:Name="ColorPicker" Source="ColorWheel.png" HorizontalAlignment="Center"/>
    </Grid>
</TabItem>