時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