开发者

Dictionary resources and Binding

I need access to borderbrush from code, how can bind?

This not work:

BorderBrush="{Binding color}"

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

Here my 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>
                        </Rep开发者_高级运维eatButton.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>


Like this?

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

If you need to bind to another property, then:

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

[Edit] Attempt #3 :)

Not sure I understand completely, but this is probably what you are looking for:

BorderBrush="{TemplateBinding BorderBrush}"

Now set BorderBrush of your RepeatButton and it should work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜