如何在WPF中使用Ellipse数据网格 [英] How to data grid with Ellipse in WPF

查看:361
本文介绍了如何在WPF中使用Ellipse数据网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我想创建一个表来显示哪个用户在他们的系统中打开了哪个软件。



要显示用户是否拥有该软件,我想创建一个省略号,如果为真,则为绿色,如果为假则为红色。



我应该对代码没有任何问题,我现在想弄清楚(因为我之前从未使用过WPF)是最好的方法。



我想我应该创建一个数据网格,以便对网格本身的外观进行最佳控制,因为我希望网格的线条出现。但是,datagrids不允许我在他们的单元格中插入省略号。



然后我尝试使用Grid,这很好用,但是我找不到可行的方法自定义网格线的外观。我只能找到显示或不显示网格线的选项,我想指定颜色,宽度,样式等。



那我该怎么办?有什么建议吗?



谢谢

解决方案

您好。您可以执行以下操作。您的DataGridColumn必须如下:

 <   DataGridTemplateColumn    标题  = 状态 >  
< DataGridTemplateColumn.CellTemplate >
< DataTemplate >
< 按钮 >
< Button.Template >
< ControlTemplate TargetType = {x:Type Button} >
< Ellipse 填充 = {TemplateBinding Background} 宽度 = 10 高度 = 10 / >
< / ControlTemplate >
< / Button.Template >
< Button.Style >
< 样式 TargetType = {x:类型按钮} >
< EventSetter 事件 = Button.Loaded 处理程序 = StatusButtonHandler / >
< / Style >
< / Button.Style >
< /按钮 >
< / DataTemplate >
< / DataGridTemplateColumn.CellTemplate >
< / DataGridTemplateColumn >





和你的代码在你身后可以定义一个属性来检查用户软件的状态。

在这里我定义了一个IsLoad属性:



 私有  void  StatusButtonHandler( object  sender,RoutedEventArgs e)
{
Button source = e.OriginalSource as 按钮;
if this .IsLoad == true
{
source.Background = Brushes.Green;
}
else
{
source.Background = Brushes.Red;
}
}





我的IsLoad财产:

  public   bool  IsLoad 
{
获得;
set ;
}





我希望这可以帮助你。

祝你好运


您好。我认为DataGridCheckBoxColumn。这更专业:

 <   DataGridCheckBoxColumn    绑定  =  {Binding IsFlagged} >  
< DataGridCheckBoxColumn.HeaderTemplate >
< DataTemplate > ;
< 图像 来源 = / Images / BlueFlag.png 宽度 = 24 高度 = 24 / >
< / DataTemplate >
< / DataGridCheckBoxColumn.HeaderTemplate >
< DataGridCheckBoxColumn.ElementStyle >
< 样式 TargetType = {x:键入CheckBox} >
< Setter 属性 = 高度 = 10 / >
< Setter
属性 = 宽度 = 10 / >
< Setter 属性 = 模板 >
< Setter.Value >
< ControlTemplate TargetType = {x:Type CheckBox} >
< Ellipse x :名称 = FlagEllipse 填充 = LightGray < span class =code-attribute> 宽度 = 10 高度 = 10 / >
< ControlTemplate.Triggers >
< 触发器 属性 = IsChecked = True >
< Setter TargetNam e = FlagEllipse 属性 = 填写 = 红色 / >
< / Trigger >
< / ControlTemplate.Triggers >
< / ControlTemplate
>
< / Setter .Value >
< / Setter >
< / Style >
< / DataGridCheckBoxColumn.ElementStyle >
< / DataGridCheckBoxColumn >





IsFlagged是您检查的财产。

Hi,

I''d like to create a table to show which user has which software opened in their system.

To show if the user has the software or not, I''d like to create an ellipsis that would be colored green if true and red if false.

I should have no problem with the code, what i''m now trying to figure out (since i''ve never used WPF before) is the best approach.

I think I should create a data grid to have optimal control over the look of the grid itself, as i want the lines of the grid to appear. However, datagrids don''t allow me to insert ellipsis in their cells.

I then tried using the Grid, which works great, but I found no possible way to customize the appearance of the grid lines. I can only find the option to show or not show grid lines, and I would like to specify color, width, style, etc.

So what should I do? Any suggestions?

Thanks

解决方案

Hi.you can do following.your DataGridColumn must be like:

<DataGridTemplateColumn Header="Status">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Button>
                                <Button.Template>
                                    <ControlTemplate TargetType="{x:Type Button}">
                                        <Ellipse Fill="{TemplateBinding Background}" Width="10" Height="10"/>
                                    </ControlTemplate>
                                </Button.Template>
                                <Button.Style>
                                    <Style TargetType="{x:Type Button}">
                                        <EventSetter Event="Button.Loaded" Handler="StatusButtonHandler"/>
                                    </Style>
                                </Button.Style>
                            </Button>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>



and in your codeBehind you can define a property for checking the status of users'' software.
in here i define a IsLoad property:

private void StatusButtonHandler(object sender, RoutedEventArgs e)
        {
            Button source = e.OriginalSource as Button;
            if (this.IsLoad == true)
            {
                source.Background = Brushes.Green;
            }
            else
            {
                source.Background = Brushes.Red;
            }
        }



my IsLoad property:

public bool IsLoad
       {
           get;
           set;
       }



I hope this help you.
Good Luck


Hi. i credate a DataGridCheckBoxColumn.this is more professional:

<DataGridCheckBoxColumn Binding="{Binding IsFlagged}">
                                <DataGridCheckBoxColumn.HeaderTemplate>
                                    <DataTemplate >
                                        <Image Source="/Images/BlueFlag.png" Width="24" Height="24"/>
                                    </DataTemplate>
                                </DataGridCheckBoxColumn.HeaderTemplate>
                                <DataGridCheckBoxColumn.ElementStyle>
                                    <Style TargetType="{x:Type CheckBox}">
                                        <Setter Property="Height" Value="10"/>
                                        <Setter Property="Width" Value="10"/>
                                        <Setter Property="Template">
                                            <Setter.Value>
                                                <ControlTemplate TargetType="{x:Type CheckBox}">
                                                    <Ellipse x:Name="FlagEllipse" Fill="LightGray" Width="10" Height="10"/>
                                                    <ControlTemplate.Triggers>
                                                        <Trigger Property="IsChecked" Value="True">
                                                            <Setter TargetName="FlagEllipse" Property="Fill" Value="Red"/>
                                                        </Trigger>
                                                    </ControlTemplate.Triggers>
                                                </ControlTemplate>
                                            </Setter.Value>
                                        </Setter>
                                    </Style>
                                </DataGridCheckBoxColumn.ElementStyle>
                            </DataGridCheckBoxColumn>



"IsFlagged " is your property for checking.


这篇关于如何在WPF中使用Ellipse数据网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆