时MVVM没有设置路径风格,我有我标签项声明如下:WPF - 结合
<TabItem >
<TabItem.HeaderTemplate>
<DataTemplate DataType="pricefiles:ConfigureViewModel">
<StackPanel Orientation="Horizontal">
<Path Style="{Binding StandardEnabled}"/>
<TextBlock Text="Standard" Margin="5" />
</StackPanel>
</DataTemplate>
</TabItem.HeaderTemplate>
<Grid Background="#FFE5E5E5">
<Standard:StandardView />
</Grid>
</TabItem>
然后在我的视图模型我有:
private Style _standardEnabled;
public Style StandardEnabled
{
get { return _standardEnabled; }
set { SetProperty(ref _standardEnabled, value); }
}
public ConfigureViewModel(SYSTEM_Account account)
{
try
{
Account = account;
StandardEnabled = (Style) Application.Current.FindResource("HeadersIcon");
}
catch (Exception ex)
{
}
}
风格是在一个资源字典宣称为:
<Style x:Key="HeadersIcon" TargetType="Path">
<Setter Property="Data" Value="M0,13.333001L35.555001,13.333001 35.555001,33.778223C35.555001,35.250969,34.36089,36.445002,32.88818,36.445002L2.666523,36.445002C1.1938604,36.445002,2.4359167E-07,35.250969,0,33.778223z M3.2485678,6.19415L3.2485678,8.0274701 22.415489,8.0274701 22.415489,6.19415z M3.2485678,2.6108398L3.2485678,4.44415 22.415489,4.44415 22.415489,2.6108398z M2.666523,0L32.88818,0C34.36089,0,35.555001,1.19415,35.555001,2.6666899L35.555001,10.222 0,10.222 0,2.6666899C2.4359167E-07,1.19415,1.1938603,0,2.666523,0z" />
<Setter Property="RenderTransformOrigin" Value="0.5,0.5"></Setter>
<Setter Property="Stretch" Value="Uniform"></Setter>
<Setter Property="Fill" Value="#FF000000"></Setter>
<Setter Property="Width" Value="16"></Setter>
<Setter Property="Height" Value="16"></Setter>
</Style>
当这段代码触发时,我可以看到StandardEnabled属性内的资源(样式),但是然后它不会将数据绑定到Path。
我打算根据不同的情况用不同的资源绑定数据。
编辑:
当用户按下一个父窗口上的按钮的视图/视图模型被初始化:
try
{
var tmpVm = new ConfigureViewModel(SystemAccount);
var tmp = new ConfigureView(tmpVm);
tmp.ShowDialog();
}
catch (Exception ex)
{
}
要初始化数据上下文:
public ConfigureView(ConfigureViewModel configureViewModel)
{
InitializeComponent();
DataContext = configureViewModel;
}
此代码片段是否完整?你真的在某个地方实例化一个'ConfigureViewModel'并将它分配给'TabItem.Header'属性吗? – Michael
请参阅我的编辑 – Houlahan
为什么要在视图模型中执行'SetProperty()'?这有点太紧密,IMHO。只需设置私有变量并继续......我想知道这是否能解决您的问题。 – code4life