Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
WPF
SmartUI
提交
a163c4a3
S
SmartUI
项目概览
WPF
/
SmartUI
通知
3
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
SmartUI
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
a163c4a3
编写于
3月 18, 2021
作者:
柯
柯勇
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Cascader Text数据绑定
上级
f3dda801
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
70 addition
and
20 deletion
+70
-20
SmartUI.Demo/MainWindow.xaml
SmartUI.Demo/MainWindow.xaml
+1
-1
SmartUI/Controls/Cascader.cs
SmartUI/Controls/Cascader.cs
+69
-19
未找到文件。
SmartUI.Demo/MainWindow.xaml
浏览文件 @
a163c4a3
...
...
@@ -130,7 +130,7 @@
<ComboBoxItem Content="选项四"/>
<ComboBoxItem Content="选项五"/>
</smart:ComboBoxControl>
<smart:Cascader ItemsSource="{Binding CascaderItemSource}">
<smart:Cascader ItemsSource="{Binding CascaderItemSource}"
Text="选项一/子项一"
>
</smart:Cascader>
</WrapPanel>
...
...
SmartUI/Controls/Cascader.cs
浏览文件 @
a163c4a3
...
...
@@ -32,16 +32,62 @@ namespace SmartUI.Controls
return
;
}
cascader
.
RaiseItems
(
source
);
if
(!
string
.
IsNullOrEmpty
(
cascader
.
Text
))
cascader
.
ResetByText
();
}
public
new
string
Text
{
get
{
return
(
string
)
GetValue
(
TextProperty
);
}
set
{
SetValue
(
TextProperty
,
value
);
}
}
public
static
new
readonly
DependencyProperty
TextProperty
=
DependencyProperty
.
Register
(
nameof
(
Text
),
typeof
(
string
),
typeof
(
Cascader
),
new
FrameworkPropertyMetadata
(
string
.
Empty
,
FrameworkPropertyMetadataOptions
.
BindsTwoWayByDefault
,
TextChanged
));
private
static
void
TextChanged
(
DependencyObject
d
,
DependencyPropertyChangedEventArgs
e
)
{
Cascader
control
=
d
as
Cascader
;
if
(
control
.
IsDropDownOpen
)
return
;
control
.
ResetByText
();
}
pr
otected
override
void
OnSelectionChanged
(
SelectionChangedEventArgs
e
)
pr
ivate
void
ResetByText
(
)
{
base
.
OnSelectionChanged
(
e
);
IsDropDownOpen
=
true
;
string
text
=
Text
;
int
index
=
0
;
if
(
ItemsSource
?.
Count
>
0
&&
!
string
.
IsNullOrWhiteSpace
(
text
))
{
ObservableCollection
<
CascaderItem
>
items
=
ItemsSource
;
CascaderItem
selectItem
=
null
;
do
{
foreach
(
var
item
in
items
)
{
item
.
ParentTree
=
selectItem
;
if
(
text
.
StartsWith
(
item
.
DisplayName
))
{
item
.
IsChecked
=
true
;
text
=
text
.
Substring
(
item
.
DisplayName
.
Length
).
TrimStart
(
'/'
);
break
;
}
}
selectItem
=
items
.
FirstOrDefault
(
p
=>
p
.
IsChecked
);
if
(
_itemsPanle
.
Children
.
Count
<=
index
)
{
RaiseItems
(
items
);
}
else
{
(
_itemsPanle
.
Children
[
index
]
as
ListBox
).
ItemsSource
=
items
;
}
index
++;
items
=
selectItem
?.
Children
;
}
while
(
items
!=
null
);
}
}
public
override
void
OnApplyTemplate
()
...
...
@@ -52,6 +98,8 @@ namespace SmartUI.Controls
RaiseItems
(
ItemsSource
);
_border
=
GetTemplateChild
(
"border"
)
as
Border
;
_border
.
MouseLeftButtonDown
+=
Border_MouseLeftButtonDown
;
if
(!
string
.
IsNullOrEmpty
(
Text
))
ResetByText
();
}
private
void
RaiseItems
(
ObservableCollection
<
CascaderItem
>
source
)
...
...
@@ -76,27 +124,29 @@ namespace SmartUI.Controls
model
.
IsChecked
=
true
;
if
(
model
.
Children
is
null
||
model
.
Children
.
Count
==
0
)
{
IsDropDownOpen
=
false
;
if
(
index
+
1
<
_itemsPanle
.
Children
.
Count
)
_itemsPanle
.
Children
.
RemoveRange
(
index
+
1
,
_itemsPanle
.
Children
.
Count
-
index
-
1
);
this
.
Text
=
GetParentNames
(
model
);
this
.
SelectedItem
=
model
.
Data
;
return
;
}
IsDropDownOpen
=
true
;
foreach
(
var
item
in
model
.
Children
)
{
item
.
ParentTree
=
model
;
}
if
(
_itemsPanle
.
Children
.
Count
==
index
+
1
)
{
RaiseItems
(
model
.
Children
);
this
.
SelectedItem
=
model
;
IsDropDownOpen
=
false
;
}
else
if
(
_itemsPanle
.
Children
.
Count
-
1
>
index
)
else
{
(
_itemsPanle
.
Children
[
index
+
1
]
as
ListBox
).
ItemsSource
=
model
.
Children
;
if
(
index
+
2
<
_itemsPanle
.
Children
.
Count
)
_itemsPanle
.
Children
.
RemoveRange
(
index
+
2
,
_itemsPanle
.
Children
.
Count
-
index
-
2
);
IsDropDownOpen
=
true
;
foreach
(
var
item
in
model
.
Children
)
{
item
.
ParentTree
=
model
;
}
if
(
_itemsPanle
.
Children
.
Count
==
index
+
1
)
{
RaiseItems
(
model
.
Children
);
}
else
if
(
_itemsPanle
.
Children
.
Count
-
1
>
index
)
{
(
_itemsPanle
.
Children
[
index
+
1
]
as
ListBox
).
ItemsSource
=
model
.
Children
;
if
(
index
+
2
<
_itemsPanle
.
Children
.
Count
)
_itemsPanle
.
Children
.
RemoveRange
(
index
+
2
,
_itemsPanle
.
Children
.
Count
-
index
-
2
);
}
}
if
(
_itemsPanle
.
Children
.
Count
>
0
)
(
_itemsPanle
.
Children
[
_itemsPanle
.
Children
.
Count
-
1
]
as
ListBox
).
BorderThickness
=
new
Thickness
(
0
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录