开发者

changing text colour using system colours from a combo box

I am in the process of making a text editor, I am trying to set up some开发者_开发技巧 functionality so that the user can select a colour from a combo box and that will change the colour of the text. Right now my combo box is being loaded with the system colours in xml using a resource like so

<ToolBarTray.Resources>
<ObjectDataProvider MethodName="GetType" ObjectType="{x:Type sys:Type}" x:Key="colorsTypeOdp">
<ObjectDataProvider.MethodParameters>
<sys:String>System.Windows.Media.Colors, PresentationCore, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35</sys:String>
 </ObjectDataProvider.MethodParameters>
 </ObjectDataProvider>

<ObjectDataProvider ObjectInstance="{StaticResource colorsTypeOdp}"    MethodName="GetProperties" x:Key="colorPropertiesOdp">
</ObjectDataProvider>
</ToolBarTray.Resources>

<ComboBox Name="colors" ItemsSource="{Binding Source={StaticResource colorPropertiesOdp}}" DisplayMemberPath="Name" SelectedValuePath="Name" MinWidth="100" ToolTip="Color" />

I am trying to make a selectionChanged event code that will change the text to the system colour selected by the user, if you need to see more code or need more info let me know. The comboBox is just being loaded with the name of the colour, so HOW do I use that name to get the actual color itself in the event code to set the text to the new colour? Thanks, Beef


Here is an example of binding (using the value of the combobox to fill a rectangle), and below is a an example of changing the color of the TextBlock that labels it (you would obviously update your selected text instead).

Binding:

<StackPanel Orientation="Horizontal"
            xmlns:sys="clr-namespace:System;assembly=mscorlib">
    <StackPanel.Resources>
        <ObjectDataProvider MethodName="GetType" 
                            ObjectType="{x:Type sys:Type}" 
                            x:Key="colorsTypeOdp">
            <ObjectDataProvider.MethodParameters>
                <sys:String>System.Windows.Media.Colors, PresentationCore, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35</sys:String>
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
        <ObjectDataProvider ObjectInstance="{StaticResource colorsTypeOdp}"
                            MethodName="GetProperties" 
                            x:Key="colorPropertiesOdp" /> 
    </StackPanel.Resources>
    <!--  SelectedValuePath="Name" -->
    <ComboBox Name="colors" 
              ItemsSource="{Binding Source={StaticResource colorPropertiesOdp}}" 
              DisplayMemberPath="Name" 
              SelectedValuePath="Name"
              MinWidth="100" 
              ToolTip="Color" />
    <Rectangle Width="100"
               Height="50"
               Stroke="White"
               StrokeThickness="2">
        <Rectangle.Fill>
            <SolidColorBrush Color="{Binding ElementName=colors, Path=SelectedValue}" />
        </Rectangle.Fill>
    </Rectangle>
    <TextBlock x:Name="txtColor" 
               Foreground="White"
               Text="{Binding ElementName=colors, Path=SelectedValue}" />
</StackPanel>

Event:

    colors.SelectionChanged += (s, e) => 
    {
        BrushConverter converter = new BrushConverter();
        txtColor.Foreground = converter.ConvertFromString(colors.SelectedValue.ToString()) as SolidColorBrush;
    };
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜