html中自定义菜单栏,WPF 自定义标题栏 自定义菜单栏

标签:

原文:WPF 自定义标题栏 自定义菜单栏

自定义标题栏

自定义列表,可以直接修改WPF中的ListBox模板,也用这样类似的效果。但是ListBox是不能设置默认选中状态的。

而我们需要一些复杂的UI效果,还是直接自定义控件来的快

249b46da2b2cbc21efccfbe76832c531.png

一、设计界面样式

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

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"

mc:Ignorable="d"

d:DesignHeight="200" d:DesignWidth="800" Loaded="TitleListControl_OnLoaded" >

View Code

二、控件后台代码

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

public partial class TitleListControl : UserControl

{

public TitleListControl()

{

InitializeComponent();

}

///

/// get or set the items

///

public List TitleListItems

{

get { return (List) GetValue(TitleListItemsProperty); }

set{SetValue(TitleListItemsProperty,value);}

}

public static readonly DependencyProperty TitleListItemsProperty = DependencyProperty.Register("TitleListItems", typeof(List),

typeof(TitleListControl),new PropertyMetadata(new List()));

public UIElementCollection Items

{

get { return SpTitleList.Children; }

}

private void TitleListControl_OnLoaded(object sender, RoutedEventArgs e)

{

if (TitleListItems!=null)

{

var items = TitleListItems;

int index = 0;

foreach (var item in items)

{

var radiaoButton=new RadioButton()

{

Content = item.Name

};

if (index == 0)

{

radiaoButton.Style = GetStyle("first");

}

else if (index == items.Count - 1)

{

radiaoButton.Style = GetStyle("last");

}

item.Index = index;

radiaoButton.DataContext = item;

radiaoButton.Checked += ToggleButton_OnChecked;

SpTitleList.Children.Add(radiaoButton);

index++;

}

}

}

private Style GetStyle(string type)

{

Style style = null;

switch (type)

{

case "first":

{

style = this.Resources["FirstButtonStyle"] as Style;

}

break;

case "last":

{

style = this.Resources["LastButtonStyle"] as Style;

}

break;

}

return style;

}

private void ToggleButton_OnChecked(object sender, RoutedEventArgs e)

{

var radioButton=sender as RadioButton;

var dataModel=radioButton.DataContext as TitleListItemModel;

int index = dataModel.Index;

int count = SpTitleList.Children.Count;

var linerBrush = new LinearGradientBrush(){StartPoint=new Point(0,1),EndPoint = new Point(1,1)};

if (index==0)

{

linerBrush.GradientStops.Add(new GradientStop()

{

Color = Colors.White,

Offset = 0.2

});

linerBrush.GradientStops.Add(new GradientStop()

{

Color = Colors.DeepSkyBlue,

Offset = 1

});

}

else if (index == count - 1)

{

linerBrush.GradientStops.Add(new GradientStop()

{

Color = Colors.DeepSkyBlue,

Offset = 0

});

linerBrush.GradientStops.Add(new GradientStop()

{

Color = Colors.White,

Offset = 0.8

});

}

else

{

double offsetValue = Convert.ToDouble(index) / count;

linerBrush.GradientStops.Add(new GradientStop()

{

Color = Colors.DeepSkyBlue,

Offset = 0

});

linerBrush.GradientStops.Add(new GradientStop()

{

Color = Colors.White,

Offset = offsetValue

});

linerBrush.GradientStops.Add(new GradientStop()

{

Color = Colors.DeepSkyBlue,

Offset = 1

});

}

ControlBorder.Background = linerBrush;

}

}

public class TitleListItemModel

{

public int Index { get; set; }

public string Name { get; set; }

public string Remark { get; set; }

}

View Code

三、引用UserControl

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

xmlns:wpfApplication6="clr-namespace:WpfApplication6"

Title="MainWindow" Height="350" Width="800" Background="LightGray">

View Code

如需要控件的SelectionChanged方法,在UserControl中添加个委托或者注册一个事件即可。

标签:

来源: https://www.cnblogs.com/lonelyxmas/p/9697815.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值