字典资源和绑定

时间:2011-06-11 20:00:54

标签: c# binding

我需要从代码访问borderbrush,怎么绑定?

这不起作用:

BorderBrush="{Binding color}"

<Border x:Name="brd"  Background="Black" BorderBrush="White" BorderThickness="2" CornerRadius="3">

这是我的xaml。

<UserControl x:Class="window6.Kbd"
         x:Name="teclado"
         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:fps="clr-namespace:FPS.VirtualKeyboard;assembly=FPS.VirtualKeyboard"
         mc:Ignorable="d" d:DesignWidth="418"  Width="Auto" d:DesignHeight="177.326" Height="Auto" >
<Grid d:LayoutOverrides="VerticalMargin">
    <fps:VirtualKeyboard  x:Name="virtualKeyboard"    BorderBrush="{Binding Color1}"  >

        <fps:VirtualKeyboard.Resources>

            <fps:GetCharacterDependsOnSpecialKeysConverter x:Key="GetCharacterDependsOnSpecialKeysConverter"/>


            <DataTemplate DataType="{x:Type fps:RegularKey}">
                <Canvas>
                    <RepeatButton 

    Focusable="False"
    Content="{Binding .}"
    Canvas.Left="{Binding Left}"
    Canvas.Top="{Binding Top}"
    Width="{Binding Width}"
    Height="{Binding Height}"
    Command="fps:VirtualKeyboard.ClickCommand">
                        <RepeatButton.ContentTemplate>
                            <DataTemplate>
                                <Grid>
                                    <ContentControl Content="{Binding ., Converter={StaticResource GetCharacterDependsOnSpecialKeysConverter}}">
                                        <ContentControl.ContentTemplate>
                                            <DataTemplate>
                                                <TextBlock Text="{Binding .}"/>
                                            </DataTemplate>
                                        </ContentControl.ContentTemplate>
                                    </ContentControl>
                                </Grid>
                            </DataTemplate>
                        </RepeatButton.ContentTemplate>
                    </RepeatButton>
                </Canvas>
            </DataTemplate>
            <DataTemplate DataType="{x:Type fps:SpecialKey}">
                <Canvas>
                    <RepeatButton x:Name="btn"  Focusable="False" Content="{Binding .}" Canvas.Left="{Binding Left}" Canvas.Top="{Binding Top}" Width="{Binding Width}" Height="{Binding Height}"   Command="fps:VirtualKeyboard.ClickCommand">
                        <RepeatButton.ContentTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Content}" TextWrapping="Wrap"/>
                            </DataTemplate>
                        </RepeatButton.ContentTemplate>
                    </RepeatButton>
                </Canvas>
                <DataTemplate.Triggers>
                    <DataTrigger Binding="{Binding IsLocked}" Value="True">
                        <Setter Property="BorderBrush"  Value="Yellow" TargetName="btn"/>
                    </DataTrigger>
                </DataTemplate.Triggers>
            </DataTemplate>
            <DataTemplate DataType="{x:Type fps:NumPadKey}">
                <Canvas>
                    <RepeatButton x:Name="btn" Focusable="False" Content="{Binding .}" Canvas.Left="{Binding Left}" Canvas.Top="{Binding Top}" Width="{Binding Width}" Height="{Binding Height}"   Command="fps:VirtualKeyboard.ClickCommand">
                        <RepeatButton.ContentTemplate>
                            <DataTemplate>
                                <StackPanel HorizontalAlignment="Left">
                                    <TextBlock Text="{Binding NumCase}" Foreground="Red" HorizontalAlignment="Left"/>
                                    <TextBlock Text="{Binding Content}" TextWrapping="Wrap"  HorizontalAlignment="Left"/>
                                </StackPanel>
                            </DataTemplate>
                        </RepeatButton.ContentTemplate>
                    </RepeatButton>
                </Canvas>
                <DataTemplate.Triggers>
                    <DataTrigger Binding="{Binding IsLocked}" Value="True">
                        <Setter Property="BorderBrush"  Value="Red" TargetName="btn"/>
                    </DataTrigger>
                </DataTemplate.Triggers>
            </DataTemplate>




            <Style  TargetType="{x:Type RepeatButton}"   >
                <Setter Property="Foreground" Value="White"   />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type RepeatButton}">

                            <Grid >

                                <Border x:Name="brd"  Background="Black" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="2" CornerRadius="3">
                                    <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" RecognizesAccessKey="True"/>
                                </Border>
                            </Grid>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsFocused" Value="True"/>
                                <Trigger Property="IsMouseOver" Value="True">

                                </Trigger>
                                <Trigger Property="IsPressed" Value="True">
                                    <Setter Property="Background" TargetName="brd" Value="Gray"/>
                                </Trigger>
                                <Trigger Property="IsEnabled" Value="False"/>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>

        </fps:VirtualKeyboard.Resources>


    </fps:VirtualKeyboard>
</Grid>

1 个答案:

答案 0 :(得分:0)

喜欢这个吗?

brd.BorderBrush = new SolidColorBrush(Colors.Blue);

如果您需要绑定到另一个属性,那么:

brd.SetBinding(Border.BorderBrushProperty, new Binding("SomeProperty") { Source = someObject });

[编辑]尝试#3:)

我不确定我完全理解,但这可能就是你要找的东西:

BorderBrush="{TemplateBinding BorderBrush}"

现在设置BorderBrush的{​​{1}},它应该有效。