2013-07-17 17 views
3

我想创建一个类似于Android屏幕的窗口,显示蓝牙,wifi ..等等的状态。我有一个类,它检查所述设备的状态并返回一个0的字节值off,1个on和0xFF的错误。然后我做了一个按钮(或许应该ToggleButton..but我很新的WPF)如何使用扩展自定义控件并在窗口中使用

public class ToggleTaskButton : System.Windows.Controls.Primitives.ButtonBase 
{ 
    public ImageSource ImageSource 
    { 
     get { return (ImageSource)GetValue(ImageSourceProperty); } 
     set { SetValue(ImageSourceProperty, value); } 
    } 

    public Color MyBackgroundColor 
    { 
     get { return (Color)GetValue(MyBackgroundColorProperty); } 
     set { SetValue(MyBackgroundColorProperty, value); } 
    } 

    // Using a DependencyProperty as the backing store for MyBackgroundColor. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty MyBackgroundColorProperty = 
     DependencyProperty.Register("MyBackgroundColor", typeof(Color), typeof(ToggleTaskButton), new PropertyMetadata(null)); 


    // Using a DependencyProperty as the backing store for ImageSource. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty ImageSourceProperty = 
     DependencyProperty.Register("ImageSource", typeof(ImageSource), typeof(ToggleTaskButton), new UIPropertyMetadata(null)); 
} 

的注意事项关于上面类是我在想,我不想依赖项属性? ?相反,我宁愿设置一个值,背景颜色会更改为适当的颜色。 0将是灰色的,1将是绿色> 1将是红色。有一件事我不知道该怎么办

然后,我做了一个蓝牙用户控件,然后将类型更改为ToggleTaskButton。这个项目只是一个类库,所以我没有得到一个资源字典:/我试图让我的按钮点击部分正常工作之前,我发布了。对不起,一塌糊涂。

<ata:ToggleTaskButton x:Class="AdvancedTaskAssigner.Controls.BluetoothControl" 
      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:ata="clr-namespace:AdvancedTaskAssigner.Controls" 
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300" Loaded="UserControl_Loaded" Click="ToggleTaskButton_Click" 
         ImageSource="/AdvancedTaskAssigner;component/Resources/Bluetooth.png" MyBackgroundColor="Green"> 
    <ata:ToggleTaskButton.Resources> 
     <Style TargetType="{x:Type ata:ToggleTaskButton}"> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="{x:Type ata:ToggleTaskButton}"> 
         <Viewbox> 
         <Grid> 
          <Border BorderBrush="#FF58595B" BorderThickness="15,15,15,15" CornerRadius="8,8,8,8" > 
           <Border.Background> 
            <RadialGradientBrush> 
             <GradientStop Color="#FFB2B2B2" Offset=".75"/> 
              <GradientStop Offset="1" Color="#FFB2B2B2" /> 
            </RadialGradientBrush> 
           </Border.Background> 
           <Viewbox> 
            <Image Margin="25" Height="100" Width="100" Source="{TemplateBinding ImageSource}" /> 
           </Viewbox> 
          </Border> 
         </Grid> 
         </Viewbox> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </ata:ToggleTaskButton.Resources> 
</ata:ToggleTaskButton> 

后面的代码

using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Media; 

namespace AdvancedTaskAssigner.Controls 
{ 
    /// <summary> 
    /// Interaction logic for BluetoothControl.xaml 
    /// </summary> 
    public partial class BluetoothControl : ToggleTaskButton 
    { 
     public BluetoothControl() 
     { 
      InitializeComponent(); 
      task = new BrightnessTask(); 
     } 

     private void UserControl_Loaded(object sender, RoutedEventArgs e) 
     { 
      CheckBluetoothState(); 
      System.Console.Beep(1000, 100); 
     } 

     private void CheckBluetoothState() 
     { 
      //bluetoothState = ((byte)task.GetState() == 0x01); 
      //Color c = bluetoothState ? (Color)FindResource("enabledColor") : (Color)FindResource("disabledColor"); 
      //this.outsideColor.Color = c; 
     } 

     private BrightnessTask task; 
     private bool bluetoothState = false; 

     private void ToggleTaskButton_Click(object sender, RoutedEventArgs e) 
     { 
      if (bluetoothState) { task.PerformTaskDown(); MessageBox.Show("BOO"); System.Console.Beep(1000, 100); } //if bluetooth enabled..disable 
      else { task.PerformTaskUp(); System.Console.Beep(2000, 100); MessageBox.Show("BOO"); }//if bluetooth disabled..enable. 

      CheckBluetoothState(); 
     } 
    } 
} 

所以我最终真的不知道我在做什么。我希望这是WPF,因为我需要处理各种平板电脑和各种屏幕尺寸。我在想,OnLoad控件应该使用蓝牙任务来设置状态。当状态被设置时,它会改变边界背景上第二个渐变停止的颜色。请帮忙。我如何设置GradientStop的颜色?当我在WPF应用程序这个控件添加到用户控件它说明不了什么,但在我的设计师就说明这3个按钮

enter image description here

+0

@JoeKorolewicz感谢您加入该标签。 –

+0

在prinitsipe中可以使它更容易,例如使用'Dependency Property'或'Visual State Manager'。你想为你的问题提供一个替代解决方案?或者你想像你写的那样去做? –

+0

@AnatoliyNikolaev我希望能解决我的问题。我还没有想出如何使这项工作正常进行,但我认为我正在接近一点。 –

回答

1

我创建了一个附加依赖属性CurrentStatus,其中包含的例子之一连接的当前状态。我还为Button触发器创建了一个模板,用于设置Button的属性,具体取决于状态。在Button可以有三种状态,并且添加一个新状态并不困难。的按钮风格

例子:

<Style x:Key="BlueToothButtonStyle" TargetType="{x:Type Button}"> 
    <Setter Property="Background" Value="Gainsboro" /> 
    <Setter Property="Foreground" Value="Black" /> 
    <Setter Property="FontSize" Value="15" /> 
    <Setter Property="FocusVisualStyle" Value="{x:Null}" /> 
    <Setter Property="SnapsToDevicePixels" Value="True" /> 

    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type Button}"> 
       <Border CornerRadius="2" Background="{TemplateBinding Background}"> 
        <Grid> 
         <ContentPresenter x:Name="MyContentPresenter" Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,0,0,0" /> 
         <Path x:Name="Bicon" Width="15" Height="27" Stretch="Fill" Fill="#FF000000" Data="F1 M 51,47L 36,61L 36,43L 28.25,50L 25.25,46.75L 35,38L 25.25,29.25L 28.25,26L 36,32L 36,14L 51,29L 42,38L 51,47 Z M 41,43L 41,50.5L 44.5,46.5L 41,43 Z M 41,33L 44.5,29.5L 41,25.3995L 41,33 Z "/> 
        </Grid> 
       </Border> 

       <ControlTemplate.Triggers> 
        <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=(local:DepClass.CurrentStatus)}" Value="1"> 
         <Setter Property="Background" Value="Green" /> 
        </DataTrigger> 

        <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=(local:DepClass.CurrentStatus)}" Value="2"> 
         <Setter Property="Background" Value="Red" /> 
         <Setter Property="Foreground" Value="White" /> 
        </DataTrigger> 

        <Trigger Property="IsMouseOver" Value="True"> 
         <Setter Property="Background" Value="#E59400" /> 
         <Setter TargetName="Bicon" Property="Fill" Value="White" />         
        </Trigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

这里DataTrigger设置的状态值:

<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=(local:DepClass.CurrentStatus)}" Value="1"> 
    <Setter Property="Background" Value="Green" /> 
</DataTrigger> 

触发还可以是这样的:

<Trigger Property="local:DepClass.CurrentStatus" Value="1"> 
    <Setter Property="Background" Value="#E59400" />        
</Trigger> 

这将是一样。

Output

状态0

enter image description here

状态1

enter image description here

状态2

上市

enter image description here

DependencyProperty

public class DepClass : DependencyObject 
{ 
    public static readonly DependencyProperty CurrentStatusProperty; 

    public static void SetCurrentStatus(DependencyObject DepObject, int value) 
    { 
     DepObject.SetValue(CurrentStatusProperty, value); 
    } 

    public static int GetCurrentStatus(DependencyObject DepObject) 
    { 
     return (int)DepObject.GetValue(CurrentStatusProperty); 
    } 

    static DepClass() 
    { 
     PropertyMetadata MyPropertyMetadata = new PropertyMetadata(0); 

     CurrentStatusProperty = DependencyProperty.RegisterAttached("CurrentStatus", 
                  typeof(int), 
                  typeof(DepClass), 
                  MyPropertyMetadata); 
    }   
} 

状态的定义是这样的:

DepClass.SetCurrentStatus(BluetoothButton, 1); 

或者在XAML这样的:

<Button local:DepClass.CurrentStatus="0" ... /> 

完整的示例:

XAML

<Window x:Class="BluetoothButtonHelp.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:BluetoothButtonHelp" 
    Title="MainWindow" Height="350" Width="525" 
    WindowStartupLocation="CenterScreen"> 

<Window.Resources> 
    <Style x:Key="BlueToothButtonStyle" TargetType="{x:Type Button}"> 
     <Setter Property="Background" Value="Gainsboro" /> 
     <Setter Property="Foreground" Value="Black" /> 
     <Setter Property="FontSize" Value="15" /> 
     <Setter Property="FocusVisualStyle" Value="{x:Null}" /> 
     <Setter Property="SnapsToDevicePixels" Value="True" /> 

     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type Button}"> 
        <Border CornerRadius="2" Background="{TemplateBinding Background}"> 
         <Grid> 
          <ContentPresenter x:Name="MyContentPresenter" Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,0,0,0" /> 
          <Path x:Name="Bicon" Width="15" Height="27" Stretch="Fill" Fill="#FF000000" Data="F1 M 51,47L 36,61L 36,43L 28.25,50L 25.25,46.75L 35,38L 25.25,29.25L 28.25,26L 36,32L 36,14L 51,29L 42,38L 51,47 Z M 41,43L 41,50.5L 44.5,46.5L 41,43 Z M 41,33L 44.5,29.5L 41,25.3995L 41,33 Z "/> 
         </Grid> 
        </Border> 

        <ControlTemplate.Triggers> 
         <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=(local:DepClass.CurrentStatus)}" Value="1"> 
          <Setter Property="Background" Value="Green" /> 
         </DataTrigger> 

         <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=(local:DepClass.CurrentStatus)}" Value="2"> 
          <Setter Property="Background" Value="Red" /> 
          <Setter Property="Foreground" Value="White" /> 
         </DataTrigger> 

         <Trigger Property="IsMouseOver" Value="True"> 
          <Setter Property="Background" Value="#E59400" /> 
          <Setter TargetName="Bicon" Property="Fill" Value="White" />         
         </Trigger> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</Window.Resources> 

<Grid> 
    <Button Name="BluetoothButton" Style="{StaticResource BlueToothButtonStyle}" local:DepClass.CurrentStatus="0" Width="40" Height="40" /> 

    <Button Name="Status1" Content="Status 1" Width="100" Height="30" HorizontalAlignment="Left" Click="Status1_Click" /> 
    <Button Name="Status2" Content="Status 2" Width="100" Height="30" HorizontalAlignment="Right" Click="Status2_Click" /> 
    <Button Name="Status0" Content="Status 0" Width="100" Height="30" VerticalAlignment="Top" Click="Status0_Click" /> 
</Grid> 
</Window> 

Code behind

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
    } 

    private void Status1_Click(object sender, RoutedEventArgs e) 
    { 
     DepClass.SetCurrentStatus(BluetoothButton, 1); 
    } 

    private void Status2_Click(object sender, RoutedEventArgs e) 
    { 
     DepClass.SetCurrentStatus(BluetoothButton, 2); 
    } 

    private void Status0_Click(object sender, RoutedEventArgs e) 
    { 
     DepClass.SetCurrentStatus(BluetoothButton, 0); 
    } 
} 

public class DepClass : DependencyObject 
{ 
    public static readonly DependencyProperty CurrentStatusProperty; 

    public static void SetCurrentStatus(DependencyObject DepObject, int value) 
    { 
     DepObject.SetValue(CurrentStatusProperty, value); 
    } 

    public static int GetCurrentStatus(DependencyObject DepObject) 
    { 
     return (int)DepObject.GetValue(CurrentStatusProperty); 
    } 

    static DepClass() 
    { 
     PropertyMetadata MyPropertyMetadata = new PropertyMetadata(0); 

     CurrentStatusProperty = DependencyProperty.RegisterAttached("CurrentStatus", 
                  typeof(int), 
                  typeof(DepClass), 
                  MyPropertyMetadata); 
    }   
} 
+1

哇!我对此非常惊讶。我希望你不必花很长时间就可以了。一旦我得到这个实施和它的作品,我很可能会接受你的答案,因为它看起来像我正在寻找。非常感谢你。 –

+0

不客气:)。 –

+0

@Robert Snyder:你有没有问题的例子?这可能是一些解释? –

相关问题