以编程方式将 ItemsPanelTemplate 网格位置设置为 StatusBarItem

set ItemsPanelTemplate grid position to StatusBarItem programmatically

如何以编程方式将网格位置设置到我的 StatusBarItems?

我有以下 StatusBar,现在我想在我的 ViewModel 中设置 StatusBarItems 的位置。

<StatusBar ItemsSource="{Binding StatusBarItemsSource}" DockPanel.Dock="Bottom" >
    <StatusBar.ItemsPanel>
        <ItemsPanelTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="100" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="100" />
                </Grid.ColumnDefinitions>
            </Grid>
        </ItemsPanelTemplate>
    </StatusBar.ItemsPanel>
</StatusBar>

我想在第三列显示我的徽标,但这行不通:/

StatusBarItem logo = new StatusBarItem
{
    Content = new Image
    {
        Source = new BitmapImage(new Uri("Logo.ico", UriKind.Relative)),
        Width = 16,
        Height = 16
    }
    Grid.Column = 3
};
StatusBarItemsSource.Add(logo);

使用Grid.SetColumn方法将StatusBarItemGrid.Column附加属性设置为2:

StatusBarItem logo = new StatusBarItem
{
    Content = new Image
    {
        Source = new BitmapImage(new Uri("logo.ico", UriKind.Relative)),
        Width = 16,
        Height = 16
    }
};
Grid.SetColumn(logo, 2);
StatusBarItemsSource.Add(logo);